简体   繁体   中英

How do I get Internet Explorer to parse “2010-06-09T15:20:00Z” as a date?

I am using JavaScript to work with dates in combined UTC format (eg "2010-06-09T15:20:00Z"). IE is not giving expected results (shock). The following yields NaN.

var d = new Date("2010-06-09T15:20:00Z");  // NaN

However Microsoft's documentation on this page says this should work (like it does in FF). I have even copied the JScript code off of this page and it fails to run giving the same error I am finding in my work. (Kind of makes you wonder if MS testing it before posting or if IE is broken due to a patch).

Formatting Date and Time String (msdn.microsoft.com)

I can get IE to parse similar strings by removing the 'T' and switching the dashes to slashes. The problem with this is that I loose the time's offset logic.

Any suggestions on how to work around this problem in IE. I have tested this in IE6 and IE7 (compatibility mode). I don't have another Windows system to check on.

For hassle free date manipulation I like to use Datejs - An open-source JavaScript Date Library , it does a lot of great things with dates, the main cost of using it thought is the 26kb filesize.

It will parse "2010-06-09T15:20:00Z"

Date.fromISO= (function(){
    var diso= Date.parse('2011-04-26T13:16:50Z');
    if(diso=== 1303823810000) return function(s){
        return new Date(Date.parse(s));
    }
    else return function(s){
        var day, tz, 
        rx= /^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/, 
        p= rx.exec(s) || [];
        if(p[1]){
            day= p[1].split(/\D/).map(function(itm){
                return parseInt(itm, 10) || 0;
            });
            day[1]-= 1;
            day= new Date(Date.UTC.apply(Date, day));
            if(!day.getDate()) return NaN;
            if(p[5]){
                tz= parseInt(p[5], 10)*60;
                if(p[6]) tz += parseInt(p[6], 10);
                if(p[4]== "+") tz*= -1;
                if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
            }
            return day;
        }
        return NaN;
    }
})()

I cringe at the thought of extending Javascript core objects so I tend to work with them instead. Full credit to Paul Sowden

    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var time;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    date.setTime(Number(time));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM