【发布时间】:2021-10-25 13:45:15
【问题描述】:
我已经实现了操纵日期,但我得到低于输出。您能否解释一下为什么会发生这种情况并为此提供解决方案。
HTML:
<html>
<script>
const d= new Date();
console.log(d);
let a=d;
a =new Date(a.setDate(d.getDate() + 2));
console.log(a);
console.log(d);
</script>
</html>
输出:
Wed Aug 25 2021 17:23:00 GMT+0530 (India Standard Time)
Fri Aug 27 2021 17:23:00 GMT+0530 (India Standard Time)
Fri Aug 27 2021 17:23:00 GMT+0530 (India Standard Time)
预期输出:
Wed Aug 25 2021 17:23:00 GMT+0530 (India Standard Time)
Fri Aug 27 2021 17:23:00 GMT+0530 (India Standard Time)
Wed Aug 25 2021 17:23:00 GMT+0530 (India Standard Time)
谢谢。
【问题讨论】:
-
setDate改变日期对象 -
我将 setDate 与对象 a 一起使用,而不是与 d 一起使用。那么为什么 d 会发生变化。
-
因为
a只是在您执行let a=d;之后对d的引用 -
而且你在我的评论之后编辑了你的代码!你打电话给
d.setDate
标签: javascript html date datetime object