10

Possible Duplicate:
Zero-based month numbering

Why is January month 0 in JS date object ? For instance, I would expect this snippet to create a date oject for the 8th of Februray 2013. Instead, it's March. All other fields areintuitive. Years are natural as well as day of month and time.

test_date = New Date(2013, 2, 8);

Is there any rational behind this ?

Community
  • 1
  • 1
yadutaf
  • 6,840
  • 1
  • 37
  • 48

2 Answers2

8

Because then you can have:

var monthnames = ["Jan","Feb","Mar"....];

And access the array by:

monthnames[myDate.getMonths()]

Similarly for weekdays, where Sunday is 0.

However, years, and days are just numeric values, not indices to some greater meaning.

Side-note: Hours, minutes and seconds are zero-based in "human time" too.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 2
    Sorry but month is numeric value is well. January is month 1, and December is month 12. Same as day of week. Monday is 1, Tuesday is 2, etc., and Sunday can be either 0 or 7 depending whether Sunday is at the beginning or at the end. – Michael Tsang Feb 27 '19 at 07:57
0

There are only 12 months while there can be an infinite number of years. So i would think it is more efficient to use an array of twelve to store the month value.

Ibu
  • 42,752
  • 13
  • 76
  • 103