Home > Web Front-end > JS Tutorial > Why Does JavaScript\'s `new Date()` Show October Instead of September?

Why Does JavaScript\'s `new Date()` Show October Instead of September?

DDD
Release: 2024-11-22 10:43:10
Original
1133 people have browsed it

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.
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template