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?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityMost likely the idea is, that the months are thought of as an index into an array of month names, while days are simply "counted".
new Date().getTime()
It may also be that Javascript dates were meant to mimic java dates. Java has similarly used a zero based month since the beginning.
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).
months = ['Jan', 'Feb',..., 'Nov', 'Dec']; month = months[date.getMonth()];