最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Timestamp from ISO8061 - Stack Overflow

programmeradmin0浏览0评论

I'm having a bit of an issue when dealing with getting a timestamp from an iso8061 date. For some reason it work perfectly in Chrome, but causes an Invalid Date error in Firefox. The exact line is:

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

I've tried passing the date through (as the var time) 2011-03-09T16:46:58+00:00, 2011-03-09T16:46:58+0000 and 2011-03-09T16:48:37Z as per the spec outlined but I still can't seem to get it to work in firefox. In fact, the last method didn't work in either browser.

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

Thanks, Angelo R.

I'm having a bit of an issue when dealing with getting a timestamp from an iso8061 date. For some reason it work perfectly in Chrome, but causes an Invalid Date error in Firefox. The exact line is:

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

I've tried passing the date through (as the var time) 2011-03-09T16:46:58+00:00, 2011-03-09T16:46:58+0000 and 2011-03-09T16:48:37Z as per the spec outlined http://www.jibbering./faq/#dates but I still can't seem to get it to work in firefox. In fact, the last method didn't work in either browser.

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

Thanks, Angelo R.

Share Improve this question asked Mar 9, 2011 at 16:51 Angelo R.Angelo R. 2,3411 gold badge17 silver badges22 bronze badges 2
  • What do you mean by "timestamp", exactly? – Wayne Commented Mar 9, 2011 at 17:10
  • @lwburk a unix style timestamp, essentially the number of seconds since Jan 1st 1970 – Angelo R. Commented Mar 9, 2011 at 17:20
Add a ment  | 

2 Answers 2

Reset to default 6

take a look at JavaScript ISO8601/RFC3339 Date Parser:

their code:

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;
};

and then you can use it this way:

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

probably not that fortable, but you can rewrite this function, or write another one which will return date based on ISO-8601 format

The way that the Date constructor handles string arguments differs across browsers. As the first answer to this question points out, IE recognizes hyphens, but Firefox does not, as just one example.

It's probably best to use the constructor that expects individual date parts.

发布评论

评论列表(0)

  1. 暂无评论