0

I have this calendar picker that works fine, it reads the date day I am choosing in the textbox, the only weird thing is that when I choose the date day of 9, it reads "2017-03-9" instead of "2017-03-09". What's wrong here?

JavaScript:

var CalendarTwo;

function onPopupTxtDepartureDateChanged(sender) {
        var txtDeparture = $("#txtDepartureDate");
        var date = new Date(sender.getSelectedDate());
        var textDate = date.getFullYear() + "-" + (date.getMonth() < 9 ? "0" + (date.getMonth() + 1)
            : (date.getMonth() + 1)) + "-" + (date.getDate() < 9 ? "0" + date.getDate() : date.getDate());
        CalendarTwo = sender;
        txtDeparture.val(textDate);
}
Malphai
  • 307
  • 3
  • 6
  • 21
  • This would suit better: http://stackoverflow.com/questions/8043026/javascript-format-number-to-have-2-digit – Rajesh Mar 08 '17 at 10:19

1 Answers1

1

You need to check for lt;eq;:

date.getMonth() <= 9
date.getDate() <= 9
Jai
  • 74,255
  • 12
  • 74
  • 103