d34dh0r53
d34dh0r53

Reputation: 683

MongoDB adding month to Date

So, this is driving me nuts and I'm not sure if it's a bug or if I'm missing something obvious. Can someone please explain to me why a month is added to the following statement in mongo 2.0.2

# mongo
MongoDB shell version: 2.0.2
> new Date()
ISODate("2012-02-19T04:58:56.988Z")
> new Date(2012, 02, 19, 04, 58, 56)
ISODate("2012-03-19T04:58:56Z")
> new Date(2012, 01, 19, 04, 58, 56)
ISODate("2012-02-19T04:58:56Z")

Notice how a month is added when I specify the date. Whiskey Tango Foxtrot?

Upvotes: 3

Views: 2328

Answers (1)

mu is too short
mu is too short

Reputation: 434585

MongoDB uses JavaScript as its interface language. The month in the JavaScript Date constructor is zero based. From the fine manual:

month
Integer value representing the month, beginning with 0 for January to 11 for December.

Note the 0 for January. This sort of thing is one of the reasons that MongoDB tends to use their own ISODate function instead of new Date:

[...] ISODate is a thin wrapper around the Date constructor to fix some of it's shortcomings. It returns a normal Date object with all of the normal methods that javascript Date methods support.

Upvotes: 4

Related Questions