Why does the month argument range from 0 to 11 in JavaScript's Date constructor?
The real answer to this question, is that it was copied from java.util.Date
, which also had this quirk. Proof can be found on Twitter from Brendan Eich - the guy who originally implemented JavaScript:
https://twitter.com/BrendanEich/status/481939099138654209
https://twitter.com/BrendanEich/status/771006397886533632
Brendan also indicates that it was Ken Smith of Netscape who did the porting from Java.
https://twitter.com/BrendanEich/status/771006208949891072
This happened in 1995, and JDK 1.0 was in beta. It launched in 1996. In 1997, JDK 1.1 came out which deprecated the vast majority of functions on java.util.Date
, moving them over to java.util.Calendar
, but even that still had zero-based months. Developers fed-up with this created the Joda-Time library, which ultimately led to java.time
package that's baked in to Java 8 (2014).
In short, it took 18 years for Java to get a correctly designed date/time API built-in, but JavaScript is still stuck back in the dark ages. We do indeed have excellent libraries like Luxon Moment.js, date-fns, js-joda, and others. But as of now, there is nothing more than Date
built-in to the language. Hopefully this will change in the near future with the TC39 Temporal proposal.
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
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.