Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

javascript - Different values with Date() in Chrome and IE

In this fiddle, the values for

     new Date(val[0]).getTime()

differ in Chrome and IE. The chrome values appear to be proper, Is there a workaround to get the values correct in IE.

Code:

$(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);
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...