新的Date()在Chrome和Firefox中的工作方式不同 [英] new Date() works differently in Chrome and Firefox

查看:134
本文介绍了新的Date()在Chrome和Firefox中的工作方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

var date = new Date('2013-02-27T17:00:00');
alert(date);

'2013-02-27T17:00:00'是来自服务器的JSON对象的UTC时间。

'2013-02-27T17:00:00' is UTC time in JSON object from server.

但上述代码的结果在Firefox和Chrome之间是不同的:

But the result of above code is different between Firefox and Chrome:

Firefox返回:

Firefox returns:

Wed Feb 27 2013 17:00:00 GMT+0700 (SE Asia Standard Time)

Chrome返回:

Thu Feb 28 2013 00:00:00 GMT+0700 (SE Asia Standard Time) 

这是不同的1天,正确的结果,我希望是Chrome的结果。

It's different 1 day, the correct result I would expect is the result from Chrome.

演示代码: http://jsfiddle.net/xHtqa/2/

如何解决这个问题,以获得相同的结果两个?

How can I fix this problem to get the same result from both?

推荐答案

UTC的正确格式为 2013-02-27T17:00:00Z (Z为祖鲁时间)。如果不存在,请附加 Z 。以获取正确的UTC日期时间字符串。

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.

这篇关于新的Date()在Chrome和Firefox中的工作方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆