I’ve noticed that in JavaScript, when creating a Date
, months are zero based, and days aren’t.
For example:
var foo = new Date(2012, 1, 1)
produces February 1st 2012
Why is this?
1
Most likely the idea is, that the months are thought of as an index into an array of month names, while days are simply “counted”.
8
It may also be that Javascript dates were meant to mimic java dates. Java has similarly used a zero based month since the beginning.
JDK API v1.0.2 : Date.getMonth()
2
Adding to Danny Kirchmeier’s answer above, as this article claims, here’s the reason as explained by Brendan Eich, the creator of Javascript:
For reasons I’m not aware of, that Tweet seems to no longer exist, at least not for me (not a Twitter user).
4