尝试创建未来的日期,而创建了过去的日期

Attempt to create a future Date instead created a past Date

本文关键字:日期 创建 过去 未来      更新时间:2024-03-16

我当前的日期时间是April 8th,8:52 PM,我使用的代码是:

//get current time
var now = new Date();
//get current time plus 6 hours in the future
var future = new Date(now.getFullYear(), now.getMonth(), now.getDay(), now.getHours()+6, now.getMinutes(), now.getSeconds(), 0);
//print result
console.log(future.toLocaleString());

结果是4/6/2016, 2:52:43 AM。时间实际上是正确的,但这一天不知怎么倒退了两天。我做错了什么?

.getDay()返回星期几(0-6)。

您想要的是.getDate(),它返回一个月中的哪一天。


如果你要做很多日期操作,你应该看看Moment.js

此刻你们可以做:

var future = moment.add(6, 'hours');