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

查看:37
本文介绍了new 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.

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

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