Javascript Timestamp from ISO8061

I have a problem with getting timestamp from iso8061 date. For some reason, it works fine in Chrome, but causes an Invalid Date error in Firefox. Exact line:

var date = new Date(time.replace(/-/g,"/").replace(/[TZ]/g," ")); 

I tried passing the date through (like var time) 2011-03-09T16:46:58+00:00, 2011-03-09T16:46:58+0000and 2011-03-09T16:48:37Zas per the specified description http://www.jibbering.com/faq/#dates , but I still can't get it working in firefox. In fact, the latter method did not work in any browser.

If anyone can help me turn this iso8061 date into a timestamp, that would be great.

Thanks Angelo R.

+3
source share
2 answers

JavaScript ISO8601/RFC3339 Date Parser:

:

Date.prototype.setISO8601 = function(dString){
    var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
    if (dString.toString().match(new RegExp(regexp))) {
        var d = dString.match(new RegExp(regexp));
        var offset = 0;
        this.setUTCDate(1);
        this.setUTCFullYear(parseInt(d[1],10));
        this.setUTCMonth(parseInt(d[3],10) - 1);
        this.setUTCDate(parseInt(d[5],10));
        this.setUTCHours(parseInt(d[7],10));
        this.setUTCMinutes(parseInt(d[9],10));
        this.setUTCSeconds(parseInt(d[11],10));
        if (d[12]) {
            this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
        }
        else {
            this.setUTCMilliseconds(0);
        }
        if (d[13] != 'Z') {
            offset = (d[15] * 60) + parseInt(d[17],10);
            offset *= ((d[14] == '-') ? -1 : 1);
            this.setTime(this.getTime() - offset * 60 * 1000);
        }
    }
    else {
        this.setTime(Date.parse(dString));
    }
    return this;
};

:

var today = new Date();
today.setISO8601('2008-12-19T16:39:57.67Z');

, , , ISO-8601

+5

, Date , . , IE , Firefox , .

, , .

0

All Articles