0

I am using JavaScript to work with dates in combined UTC format (e.g. "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.

TroyP
  • 11
  • 3
  • 4
    From the page you linked: "**Note**: ISO date formats are new in Internet Explorer 9 standards mode." As for workarounds, see http://stackoverflow.com/questions/5802461/javascript-which-browsers-support-parsing-of-iso-8601-date-string-with-date-pars – John Flatness Aug 16 '11 at 03:29

3 Answers3

0

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"

MikeM
  • 27,227
  • 4
  • 64
  • 80
  • I wouldn't use this at all for obtaining date objects. If however you want NLP sugar then I would recommend it A+ :) – martin Aug 16 '11 at 04:47
0
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;
    }
})()
kennebec
  • 102,654
  • 32
  • 106
  • 127
0

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));
martin
  • 2,493
  • 1
  • 20
  • 13