我在Javascript Date对象中添加Time,月份增加了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());
month参数的索引为零。因此,新的Date(2014,1)是1月1日,新的Date(2014,11,1)是12月1日。
请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
具体地说,
新日期(年,月索引 [,日[,小时[,分钟[,秒[, 毫秒]]]]]]);
还请注意,Date.getMonth()返回零索引的月份,而Date.getDate()返回原样的月份。
var date = new Date(2014,10);
console.log("Date:" + date.toLocaleString("en-US"));
console.log("getMonth(): " + date.getMonth());
console.log("getDate(): " + date.getDate());
,
一切都很好。事实是,在Date()
中,月份是从0
开始计算的,因此10
不是十月,而是十一月。参见this