新日期(...)增加了一个月

oha*_*nho 1 javascript node.js

我正在使用node.js开发,我正在尝试在服务器上创建一个日期对象.

编码时:

var birthyear = 2000;
var birthmonth = 7;
var birthday = 24;
var date = new Date(birthyear, birthmonth, birthday);
console.log(date);
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

Thu Aug 24 2000 00:00:00 GMT+0300 (Jerusalem Daylight Time)
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我是8月而不是7月.

我该如何解决这个问题?

hex*_*ide 6

Date()构造函数中的month参数不是从1开始为1月,而是从0开始.因此,提供月份值7将为您提供8个月,即8月.

来自MDN:

month:表示月份的整数值,从1月的0开始到12月的11.