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)
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)
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.
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.