javascript node.js 为“new Date()”返回 GMT 时间而不是本地时间。这是一个错误吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9959965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:21:32  来源:igfitidea点击:

node.js returns GMT time and not local time for "new Date()". Is that a bug?

javascriptdatetimenode.jsdate

提问by Jaseem

If not a bug, how to tell node my timezone? If bug, is this reported already?

如果不是错误,如何告诉节点我的时区?如果是错误,是否已经报告了?

采纳答案by TooTallNate

This is not a bug, just a choice of how Node decides to represent a Dateobject in its REPL. Also, as of node v0.7.x, this output has been changed to display local time, matching the browser behavior:

这不是错误,只是 Node 决定如何Date在其 REPL 中表示对象的一种选择。此外,从 node 开始v0.7.x,此输出已更改为显示本地时间,与浏览器行为相匹配:

? ~ (master) ? node
> process.version
'v0.7.7'
> new Date
Sat Mar 31 2012 15:12:13 GMT-0700 (PDT)

回答by CR Drost

If it is a bug, it does not exist in my version of Node. It is true that the Node.js REPL prefers to announce in GMT in my version:

如果它是一个错误,它在我的 Node.js 版本中不存在。在我的版本中,Node.js REPL 确实更喜欢在 GMT 中宣布:

> new Date()
Sat, 31 Mar 2012 21:51:47 GMT

But it is in fact timezone-aware, that is just not what the REPL shows when stringifying it:

但它实际上是时区感知的,这不是 REPL 在对其进行字符串化时显示的内容:

> new Date().getTimezoneOffset()
-120
> "" + new Date()
'Sat Mar 31 2012 23:51:56 GMT+0200 (CEST)'

(I am running Node.js v0.6.1 on Ubuntu.)

(我在 Ubuntu 上运行 Node.js v0.6.1。)