Home >Web Front-end >JS Tutorial >Why Does JavaScript\'s `new Date()` Show October Instead of September?
JavaScript's Date Creates October Instead of September
When using Mozilla Firefox Firebug to create a new Date object with var myDate = new Date(2012, 9, 23, 0,0,0,0), the resulting date is shown as "Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)". This may seem incorrect, as it appears to be in October instead of September.
However, this is not an error in JavaScript's Date creation. Rather, it is due to JavaScript's zero-based month numbering. In JavaScript, months are numbered starting with 0 for January, 1 for February, and so on, up to 11 for December. Therefore, in the code above, the value 9 represents October, not September.
This numbering convention is documented in the JavaScript Date constructor's documentation, which states:
month Integer value representing the month, beginning with 0 for January to 11 for December.
Therefore, to correctly create a Date object for September 23, 2012, you should use var myDate = new Date(2012, 8, 23, 0,0,0,0).
The above is the detailed content of Why Does JavaScript\'s `new Date()` Show October Instead of September?. For more information, please follow other related articles on the PHP Chinese website!