50

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
  • 7
    months = ['Jan', 'Feb',..., 'Nov', 'Dec']; month = months[date.getMonth()];
    – zzzzBov
    Dec 13, 2012 at 18:15

3 Answers 3

54

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
  • 24
    Where this makes sense from an answer stand point, it's bat crap crazy to think this logic has been around for how long now and i've never questioned it before.
    – rlemon
    Dec 13, 2012 at 15:12
  • 2
    @rlemon The fact that you've never questioned that logic before only proves that it makes sense :) Dec 13, 2012 at 15:18
  • 17
    FWIW, I never thought it made much sense. Processing dates in JavaScript was always something that made me shudder, because I'd usually forget things like this until I started debugging strange results. Dec 13, 2012 at 15:20
  • 2
    FWIW, I am not a front end developer and don't often have to deal with dates in Javascript aside from the occasional new Date().getTime()
    – rlemon
    Dec 13, 2012 at 15:28
  • 22
    When it comes to Javascript, "this is why they did it" and "this makes a good sense" are two things you rarely hear together.
    – Tridus
    Dec 13, 2012 at 17:34
15

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
  • 7
    And the java spec mirrors the C tm structure. See also stackoverflow.com/questions/344380/…
    – user40980
    Dec 13, 2012 at 15:52
  • 36
    That's not to say that the Java Date API is a terribly good example of good design ;-) Dec 13, 2012 at 15:52
4

Adding to Danny Kirchmeier's answer above, as this article claims, here's the reason as explained by Brendan Eich, the creator of Javascript:

Brendan Eich on Twitter explaining why month is zero-based, and day is 1-based in 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
  • Nice find! Do you think you could add the content of the tweet to your message in case the image is deleted? Nov 30, 2021 at 15:38
  • I screenshotted the image and uploaded it on SE, so it shouldn't ever get deleted.
    – connexo
    Nov 30, 2021 at 15:47
  • @connexo Famous last words! I hope you're right. :) Nov 30, 2021 at 15:59
  • This legacy originates with Taligent. Sun licensed their Calendar implementation from IBM. Fun fact: it supports 13 months so 12 is a valid month called 'Undecimber'.
    – JimmyJames
    Nov 30, 2021 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.