Reputation: 852
Why is mongoDB creating wrong dates for me.
> start=new Date(2011,12,23)
"Mon Jan 23 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,12,24)
"Tue Jan 24 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,13,24)
"Fri Feb 24 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,11,24)
"Sat Dec 24 2011 00:00:00 GMT+0000 (UTC)"
>
Upvotes: 0
Views: 449
Reputation: 360802
Mongo's date object is the Javascript date object, and its months are 0-based (Jan = 0, Dec = 11).
2011/12/23 is actually month #1 (12-11) of year 2011+1 -> Jan 23/2012
2011/12/24 is actually month #1 (12-11) of year 2011+1 -> Jan 24/2012
2011/13/24 is actually month #2 (13-11) of year 2011+1 -> Feb 24/2012
etc...
and besides the point, there is no month 13 in the calendar, so in at least the 3rd case, you're feeding an invalid date in to begin with - it's only the JS spec is polite and tranlates that into "next year" for you that it works at all.
Upvotes: 4