来自 ISO8061 的 Javascript 时间戳

发布于 2024-10-21 02:34:37 字数 545 浏览 3 评论 0原文

我在处理从 iso8061 日期获取时间戳时遇到了一些问题。 由于某种原因,它在 Chrome 中完美运行,但在 Firefox 中导致无效日期错误。确切的行是:

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

我尝试通过(作为 var 时间)2011-03-09T16:46:58+00:002011-03-09T16:46 传递日期:58+00002011-03-09T16:48:37Z 根据规范概述 http://www.jibbering.com/faq/#dates 但我似乎仍然无法让它在 Firefox 中工作。事实上,最后一种方法在这两种浏览器中都不起作用。

如果有人可以帮助我将这个 iso8061 日期转换为时间戳,那就太好了。

谢谢, 安吉洛·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.com/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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女的英雄梦 2024-10-28 02:34:37

看看 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 格式的日期

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 comfortable, but you can rewrite this function, or write another one which will return date based on ISO-8601 format

情深已缘浅 2024-10-28 02:34:37

Date 构造函数处理字符串参数的方式因浏览器而异。正如 这个问题 的第一个答案指出的,IE 可以识别连字符,但 Firefox 不能,仅举一个例子。

最好使用需要单独日期部分的构造函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文