Why does this $.datepicker.parseDate("mm/yy","02/2008");
throw an InvalidDate exception?
Asked
Active
Viewed 1,124 times
1

webdreamer
- 2,359
- 3
- 23
- 30
-
02/2008 isn´t a complete date. Try 01/02/2008 – Stefan Feb 16 '12 at 10:26
-
http://stackoverflow.com/questions/5956766/jquery-datepicker-parsedate-on-mm-y-causing-invalid-date - I would point you to this question. The datepicker requires a day and a month atleast – Jibi Abraham Feb 16 '12 at 10:32
-
@Oliver, just in case you ever need to know, each y represents two digits of an year in the date format of jQuery's datepicker. So just one y would mean "08" would be a valid year. – webdreamer Feb 17 '12 at 11:38
-
@webdreamer That's interesting, it is a little non-standard. – Oliver Feb 17 '12 at 11:55
2 Answers
1
It's because "02/2008" isn't a valid date, and cannot be converted into one by javascript.
You need to pass a string in a format which includes at least a day, month and year. See the examples for more info.

Rory McCrossan
- 331,213
- 40
- 305
- 339
1
It's because in javascript a Date object must have a day as well. You could fake the day to 01 by concatenating it to the string you are trying to parse:
var date = $.datepicker.parseDate("mm/yy/dd", "02/2008" + "/01");

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
The thing is I'm trying to imlement the month selector data picker by adapting jQuery's datapicker(http://stackoverflow.com/questions/4079525/jquery-datetime-picker-need-to-pick-month-and-year-only), and I don't actually know the format; all I know is that it won't have the day. But, from your answer I was able to do var date = $.datepicker.parseDate(format+"/dd",date + "/01"); Just leaving this here for future reference. – webdreamer Feb 16 '12 at 10:35
-
@webdreamer, that's exactly what I was suggesting. That's why I deliberately split the 2 strings in my answer as I thought that the first part is variable. – Darin Dimitrov Feb 16 '12 at 10:37