javascript new Date() 在 Chrome 和 Firefox 中的工作方式不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15109894/
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
new Date() works differently in Chrome and Firefox
提问by cuongle
I want to convert date string to Date
by javascript, use this code:
我想将日期字符串转换为Date
javascript,使用以下代码:
var date = new Date('2013-02-27T17:00:00');
alert(date);
'2013-02-27T17:00:00'
is UTC time in JSON object from server.
'2013-02-27T17:00:00'
是来自服务器的 JSON 对象中的 UTC 时间。
But the result of above code is different between Firefox and Chrome:
但是上面代码的结果在 Firefox 和 Chrome 之间是不同的:
Firefox returns:
火狐返回:
Wed Feb 27 2013 17:00:00 GMT+0700 (SE Asia Standard Time)
Chrome returns:
Chrome 返回:
Thu Feb 28 2013 00:00:00 GMT+0700 (SE Asia Standard Time)
It's different 1 day, the correct result I would expect is the result from Chrome.
这是不同的 1 天,我期望的正确结果是 Chrome 的结果。
Demo code: http://jsfiddle.net/xHtqa/2/
演示代码:http: //jsfiddle.net/xHtqa/2/
How can I fix this problem to get the same result from both?
如何解决此问题以从两者获得相同的结果?
采纳答案by Petr Abdulin
The correct format for UTC would be 2013-02-27T17:00:00Z
(Z is for Zulu Time). Append Z
if not present to get correct UTC datetime string.
UTC 的正确格式是2013-02-27T17:00:00Z
(Z 代表祖鲁时间)。Z
如果不存在则追加以获取正确的 UTC 日期时间字符串。
回答by Bergi
Yeah, unfortunately the date-parsing algorithms are implementation-dependent. From the specification of Date.parse
(which is used by new Date
):
是的,不幸的是,日期解析算法依赖于实现。从(由 使用)的规范Date.parse
new Date
:
The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.
根据字符串的内容,字符串可以解释为本地时间、UTC 时间或其他时区的时间。该函数首先尝试根据日期时间字符串格式 ( 15.9.1.15) 中调用的规则解析字符串的格式。如果 String 不符合该格式,该函数可能会回退到任何特定于实现的启发式或特定于实现的日期格式。
To make the Date
constructornot (maybe) use the local timezone, use a datetime string with timezone information, e.g. "2013-02-27T17:00:00Z"
. However, it is hard to find a format that is reliable parsed by every browser - the ISO format is not recognised by IE<8 (see JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse). Better, use a unix timestamp, i.e. milliseconds since unix epoch, or use a regular expression to break the string down in its parts and then feed those into Date.UTC
.
要使Date
构造函数(可能)不使用本地时区,请使用带有时区信息的日期时间字符串,例如"2013-02-27T17:00:00Z"
. 但是,很难找到每个浏览器都能可靠解析的格式——IE<8 无法识别 ISO 格式(请参阅JavaScript:哪些浏览器支持使用 Date.parse 解析 ISO-8601 日期字符串)。更好的是,使用 unix 时间戳,即自unix epoch以来的毫秒数,或使用正则表达式将字符串分解为各个部分,然后将它们输入Date.UTC
.
回答by Sergio Abreu
I found one thing here. It seems the native Firefox Inspector Console might have a bug: If I run "new Date()" in the native Inspector, it shows a date with wrong timezone, GMT locale, but running the same command in the Firebug Extension Console, the date shown uses my correct timezone (GMT-3:00).
我在这里发现了一件事。似乎原生 Firefox Inspector 控制台可能有一个错误:如果我在原生 Inspector 中运行“new Date()”,它会显示一个带有错误时区、GMT 语言环境的日期,但在 Firebug 扩展控制台中运行相同的命令,日期显示使用我的正确时区 (GMT-3:00)。
回答by MSIslam
Try using moment.js. It goes very well and in similar fashion with all the browsers. comes with many formatting options. use moment('date').format("") instead of New Date('date')
尝试使用 moment.js。它运行得非常好,并且与所有浏览器都以类似的方式运行。带有许多格式选项。使用 moment('date').format("") 而不是 New Date('date')