【发布时间】:2020-04-04 10:30:25
【问题描述】:
NodeJS 在console.log 中输出不正确的日期值
假设:
var todayDate = new Date();
机器时区:GMT+0530 / IST
以下是结果:
#1
console.log ('Today date:', todayDate);
输出: 020-04-04T09:54:29.107Z
#2
console.log ('Today date as UTC:', todayDate.toUTCString());
输出:今天的 UTC 日期:2020 年 4 月 4 日星期六 09:54:29 GMT
#3
console.log ('Today date as string:', todayDate.toString());
输出:今天日期为字符串:Sat Apr 04 2020 15:24:29 GMT+0530(印度标准时间)
我得到的是输出#1,而预期的输出是#3。
Chrome 浏览器显示的结果与我对 NodeJS 的预期结果相同,因为它在 Chrome 引擎上运行。
请解释为什么 NodeJS 不以本地日期/时间输出。
【问题讨论】:
-
你得到了什么输出而不是预期的输出?我在节点(v12)和我的 chrome 浏览器中得到完全相同的输出,即
Sat Apr 04 2020 12:45:57 GMT+0200 (Central European Summer Time)在我的例子中。 -
我得到的是输出#1,而预期的输出是#3。
-
啊现在我明白了 - 有趣,不知道。似乎默认格式化程序在实现中有所不同。
标签: node.js