Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    });
like image 989
Srinivas Avatar asked Oct 24 '13 13:10

Srinivas


1 Answers

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')
like image 135
Jonathan Lonowski Avatar answered Sep 22 '22 22:09

Jonathan Lonowski