gpt4 book ai didi

javascript - new Date(...) 向前添加一个月

转载 作者:行者123 更新时间:2023-11-30 07:40:05 25 4
gpt4 key购买 nike

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

编码时:

var birthyear = 2000;
var birthmonth = 7;
var birthday = 24;
var date = new Date(birthyear, birthmonth, birthday);
console.log(date);

输出:

Thu Aug 24 2000 00:00:00 GMT+0300 (Jerusalem Daylight Time)

如您所见,我得到的是八月而不是七月。

我该如何解决这个问题?

最佳答案

Date() 构造函数中的月份参数不是从 1 开始表示一月份,而是从 0 开始。因此,提供月份值 7 会得到八月份,即八月份.

来自 MDN :

month: Integer value representing the month, beginning with 0 for January to 11 for December.

关于javascript - new Date(...) 向前添加一个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19750872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com