views:

66

answers:

4

When initializing a new Date object in JavaScript using the below call, I found out that the month argument counts starting from zero.

new Date(2010, 3, 1);  // that's the 1st April 2010!

Why does the month argument start from 0? On the other hand, the day of the month argument (last one) is a number from 1 to 31. Are there good reasons for this?

+1  A: 

There are always 12 months in a year, so early C implementations might have used a static fixed-width array with indexes 0..11.

Jonathan Julian
The Java Date/Calendar implementation maintains support for an extra month for some calendars. http://en.wikipedia.org/wiki/Undecimber
Pointy
+1  A: 

Everything but the day of the month is 0 based, see here for a full list including ranges :)

It's actually the 1 based days that are the oddballs here...oddly enough. Why was this was done? I don't know...but probably happened the same meeting they got plastered and decided semicolons were optional.

Nick Craver
The "ones-based" days thing is probably because no one in their right mind would ever create an array of string names for days (e.g., `{ "first", "second", "third", ..., "twenty-seventh", ... }`) and try to index it by `tm_mday`. Then again, maybe they just saw the absolute utility in making _off by one_ errors a regular occurrence.
D.Shawley
+3  A: 

It's an old (probably unfortunate, probably dying) tradition in the programming world, see the old standard (POSIX) localtime C function http://linux.die.net/man/3/localtime

leonbloy
+1  A: 

Its like this in java too.. Probably to convert int to string (0 - jan,, 1-feb), they coded this way.. because they might have an array of string (indexed from 0) of month names and these month numbers if they start from 0, it'll be lot easier to map to the month strings..

echo