12

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?

mahen23
  • 1,120
  • 1
  • 9
  • 23

3 Answers3

14

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

jk.
  • 10,216
  • 1
  • 33
  • 43
2

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)

1

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.

Walter
  • 16,158
  • 8
  • 58
  • 95
Tarik
  • 11
  • 1