我在 Javascript Date 对象中添加时间,月份增加 1。 知道逻辑到底出了什么问题。
var add_minutes = function (dt, minutes) {
return new Date(dt.getTime() + minutes*60000);
}
console.log(add_minutes(new Date(2014,10,2), 30).toString());
最佳答案
月份参数是零索引的。因此,new Date(2014,0,1) 是 1 月 1 日,new Date(2014,11,1) 是 12 月 1 日。
参见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
具体来说,
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
另请注意,Date.getMonth() 返回零索引的月份,而 Date.getDate() 按原样返回月份中的日期。
var date = new Date(2014, 0, 10);
console.log("Date:" + date.toLocaleString("en-US"));
console.log("getMonth(): " + date.getMonth());
console.log("getDate(): " + date.getDate());
关于javascript - 在 Javascript 日期对象中添加分钟后,月份得到更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58762585/