下面是以下代码片段:
date2 = new Date(1518, 8, 6, 0, 0, 0, 0);
console.log(date2);
我期望返回:
1518-09-06T00:00:00.000Z
但 console.log 返回的是:
1518-09-05T22:43:40.000Z
我在这里缺少什么?
编辑: 看来问题与node.js有关,当我用node运行代码时出现问题。
如果我在 Chrome 开发工具控制台中运行它,结果符合预期:
Fri Sep 06 1518 00:00:00 GMT+0116 (Central European Summer Time)
最佳答案
MDN documentation for Date
包括此注释:
Note: Where
Date
is called as a constructor with more than one argument, the specified arguments represent local time. If UTC is desired, usenew Date(Date.UTC(...))
with the same arguments.
换句话说,您传递的参数代表您本地时区的 1518-09-06 00:00:00,对应于 UTC 的 1518-09-05 22:43:40。这是同一时刻,只是代表在不同的时区。
关于Javascript:日期显示不正确的(?)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53694632/