3

Simple question:

Why is 8 interpreted as September and not August?

new Date(2012, 8, 30)
new Date(Date.UTC(2012, 8, 30))

// Sun Sep 30 2012 00:00:00 GMT+0100 (BST)
tompave
  • 11,952
  • 7
  • 37
  • 63
  • 9
    because months are zero based in javascript dates - it is what it is – Jaromanda X Sep 24 '15 at 10:33
  • you should ask the creator of JavaScript why he/she chose zero index months, this is not a proper question! – Rohit Kumar Sep 24 '15 at 10:35
  • 1
    See [MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters) `Integer value representing the month, beginning with 0 for January to 11 for December.` – Tushar Sep 24 '15 at 10:36

2 Answers2

4

Because the specification says so:

Months are identified by an integer in the range 0 to 11, inclusive.

And, even more specifically a little further on:

A month value of 0 specifies January; 1 specifies February; 2 specifies March; 3 specifies April; 4 specifies May; 5 specifies June; 6 specifies July; 7 specifies August; 8 specifies September; 9 specifies October; 10 specifies November; and 11 specifies December.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
1

Because the month parameter is zero based

from the docs

here Date is called as a constructor with more than one argument, if values are greater than their logical range (e.g. 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. E.g. new Date(2013, 13, 1) is equivalent to new Date(2014, 1, 1), both create a date for 2014-02-01 (note that the month is 0-based). Similarly for other values: new Date(2013, 2, 1, 0, 70) is equivalent to new Date(2013, 2, 1, 1, 10) which both create a date for 2013-03-01T01:10:00.

Jamiec
  • 133,658
  • 13
  • 134
  • 193