Chrome和IE中的Date()具有不同的值 [英] Different values with Date() in Chrome and IE
本文介绍了Chrome和IE中的Date()具有不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在此小提琴中,
In this fiddle, the values for
new Date(val[0]).getTime()
Chrome和IE。
铬值看起来是正确的,是否有一个解决方法,以在IE中获得正确的值。
differ in Chrome and IE. The chrome values appear to be proper, Is there a workaround to get the values correct in IE.
代码:
$(function () {
var text="";
$.each($.parseJSON("[{\"name\":\"critical\",\"data\":[[\"2013-10-01T00:00:00\",830],[\"2013-10-02T00:00:00\",257],[\"2013-10-03T00:00:00\",160],[\"2013-10-04T00:00:00\",200]]},{\"name\":\"normal\",\"data\":[[\"2013-10-01T00:00:00\",24],[\"2013-10-02T00:00:00\",20],[\"2013-10-03T00:00:00\",13],[\"2013-10-04T00:00:00\",30]]},{\"name\":\"ignore\",\"data\":[[\"2013-10-01T00:00:00\",1732],[\"2013-10-02T00:00:00\",1220],[\"2013-10-03T00:00:00\",1120],[\"2013-10-04T00:00:00\",1500]]}]"), function (key, value) {
$.each(value.data, function (key, val) {
text += "["+(new Date(val[0]).getTime()).toString()+","+ val[1].toString()+"]";
});
}
);
$("#container").html(text);
});
推荐答案
每个浏览器都假设部分ISO格式 - Chrome假设它们是UTC,而IE则假设它们是
Each browser is assuming a different timezone for partial ISO formats -- Chrome assumes they're in UTC while IE assumes they're local to the user.
console.log(new Date("2013-10-01T00:00:00").getUTCHours());
// Chrome: 0
// IE 10: 5 (for US/Central)
如果时区包含在格式中,例如UTC的 Z
,那么它们应该解析相同的数据:
If a timezone were included in the format, such as a Z
for UTC, then they should parse the same:
console.log(new Date("2013-10-01T00:00:00Z").getUTCHours());
// Chrome: 0
// IE 10: 0
console.log(new Date("2013-10-01T00:00:00-04:00").getUTCHours());
// Chrome: 4
// IE 10: 4
如果需要,可以在解析之前添加它:
And, if needed, you can add it just before parsing:
new Date(val[0] + 'Z')
这篇关于Chrome和IE中的Date()具有不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文