新的Date()在Chrome和Firefox中的工作方式不同

时间:2022-12-06 18:02:05

I want to convert date string to Date by javascript, use this code:

我想通过javascript将日期字符串转换为Date,使用此代码:

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:

Firefox返回:

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?

如何解决此问题以从两者中获得相同的结果?

5 个解决方案

#1


60  

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日期时间字符串。

#2


27  

Yeah, unfortunately the date-parsing algorithms are implementation-dependent. From the specification of Date.parse (which is used by new Date):

是的,遗憾的是,日期解析算法依赖于实现。从Date.parse的规范(由新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 constructor not (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。

#3


3  

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 Extension Console中运行相同的命令,日期显示使用我的正确时区(格林威治标准时间-3:00)。

#4


0  

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。与所有浏览器类似,它非常好用。附带许多格式选项。使用时刻('日期')。格式(“”)而不是新日期('日期')

#5


-1  

Noticed that FireFox wasn't returning the same result as Chrome. Looks like the format you use in kendo.toString for date makes a difference.

注意到FireFox没有返回与Chrome相同的结果。看起来你在日期的kendo.toString中使用的格式有所不同。

The last console result is what I needed:

最后的控制台结果是我需要的:

新的Date()在Chrome和Firefox中的工作方式不同

#1


60  

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日期时间字符串。

#2


27  

Yeah, unfortunately the date-parsing algorithms are implementation-dependent. From the specification of Date.parse (which is used by new Date):

是的,遗憾的是,日期解析算法依赖于实现。从Date.parse的规范(由新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 constructor not (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。

#3


3  

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 Extension Console中运行相同的命令,日期显示使用我的正确时区(格林威治标准时间-3:00)。

#4


0  

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。与所有浏览器类似,它非常好用。附带许多格式选项。使用时刻('日期')。格式(“”)而不是新日期('日期')

#5


-1  

Noticed that FireFox wasn't returning the same result as Chrome. Looks like the format you use in kendo.toString for date makes a difference.

注意到FireFox没有返回与Chrome相同的结果。看起来你在日期的kendo.toString中使用的格式有所不同。

The last console result is what I needed:

最后的控制台结果是我需要的:

新的Date()在Chrome和Firefox中的工作方式不同