创建结束时间后当前时间的非预期值

  • 本文关键字:时间 结束 创建 javascript
  • 更新时间 :
  • 英文 :


我有这些行代码:

const start = new Date();
const end = start.setDate(start.getDate() + 7);
console.log(start, new Date(), end);

end设置正确,提前一周。

start时间(即当前时间)竟然等于end时间?!

我从来没有将start分配给new Date以外的任何东西!!

为什么和如何解决这个问题?

setDate函数更改对象日期。在你的例子中,它改变了开头。

const start = new Date();
const end = new Date();
end.setDate(start.getDate() + 7);
console.log(start, new Date(), end);

Const在这里也没有意义

最新更新