Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was coding a function to remove a day from a date value in javascript and i was kind of surprise that javascript's getMonth() starts from 0 for January to 11 for December. Why javascript's getMonth() starts with 0?

share|improve this question
Because C's struct tm did it that way. – Paul Tomblin Sep 15 '11 at 12:10
OK so let me ask a more generally question. Why would you write a date object that has 0 index based months but not days,years,hours, minutes etc? – The Muffin Man Oct 3 '12 at 18:58
up vote 14 down vote accepted

at a guess, to help with indexing into arrays.

Imagine you have your month display strings stored in an array and want to get the correct string using getMonth()

of course, why arrays start at zero is another question

share|improve this answer
No no no!!! It has to do with this question – cwallenpoole Sep 15 '11 at 13:01

Most likely because the C library call invoked to get the value to return, returns months this way.

(And the reason for that design was most likely because the library call was designed along with the program to actually print the date, where a zero-based index could be used directly to print the month names)

share|improve this answer

If one element in a struct starts with 0 then all elements should start with it too or none of them. I discovered that behaviour on my site a bit late.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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