正则表达式解析 ISO-8601

发布于 2024-12-17 21:27:46 字数 1291 浏览 0 评论 0原文

作为我试图帮助解决的问题的后续: chrome 和其他浏览器中的 javascript date.parse 差异

我需要帮助来更新我在此处找到的正则表达式:

JavaScript:哪些浏览器支持使用 Date.parse 解析 ISO-8601 日期字符串

来处理 2011-11-24T09:00 :27+0200

它目前只应该处理2011-11-24T09:00:27Z 版本的 ISO 日期

rx

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

,即用于制作 这个小提琴适用于 IE 和 Safari


更新:答案有效。现在我可以帮助其他人解析从 facebook API 返回的 ISO 日期。

As a followup to a question I am trying to help with: javascript date.parse difference in chrome and other browsers

I need assistance in updating the regex I found here:

JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse

to handle 2011-11-24T09:00:27+0200

It currently only is supposed to handle the 2011-11-24T09:00:27Z version of the ISO date

i.e. the rx in

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

to make this fiddle work with IE and Safari


UPDATE: The answers worked. Now I can help others parse the ISO date returned from the facebook API.

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

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

发布评论

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

评论(2

几度春秋 2024-12-24 21:27:46

要使其适用于 2011-11-24T09:00:27+0200 格式的日期,只需在最后一个 : 之后添加 ?,例如:

/^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):?(\d\d))?$/

解释:

  (
    \d{4}\-\d\d\-\d\d     # date
    ([tT][\d:\.]*)?       # optional time
  )
  (
    [zZ]                  # UTC time zone
    |                     # or
    ([+\-])               # offset sign
    (\d\d)                # hour offset
    :?                    # optional delimiter
    (\d\d)                # minute offset
  )?                      # time zone is optional             

代码的其余部分不需要任何更改,并且该函数以前支持的所有格式仍然有效(与之前的答案不同,它破坏了四位数字的偏移量)。

To make it work with dates of the format 2011-11-24T09:00:27+0200 simply add a ? after the last :, eg:

/^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):?(\d\d))?$/

Explained:

  (
    \d{4}\-\d\d\-\d\d     # date
    ([tT][\d:\.]*)?       # optional time
  )
  (
    [zZ]                  # UTC time zone
    |                     # or
    ([+\-])               # offset sign
    (\d\d)                # hour offset
    :?                    # optional delimiter
    (\d\d)                # minute offset
  )?                      # time zone is optional             

Rest of the code shouldn't need any changes, and all previously supported formats by the function will still work (unlike the previous answer, which breaks four digit offsets).

太阳男子 2024-12-24 21:27:46

我不确定你想要什么,但你的正则表达式是错误的,请尝试更改结尾,使其看起来像这样 /^(\d{4}\-\d\d\-\d\d([tT] [\d:\.]*)?)([zZ]|([+\-])(\d{3}))?$/ 它至少会匹配你正在寻找的内容。

原始正则表达式查找字符,zZ,或者 +- 后跟 2数字,一个冒号,然后是另外 2 个数字,我更改了它,所以它不再寻找 2 个数字,一个冒号和另外 2 个数字,而是像您在示例中那样寻找 3 个数字。

I'm not sure what you want but your regex is wrong, try changing the end so it looks like this /^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d{3}))?$/ and it will at least match what you're looking for.

The original regex looked for a char, either z or Z, or a + or a - followed by 2 digits, a colon and then 2 more digits, I changed it so instead of looking for 2 digits, a colon and 2 more digits it looked for 3 digits as you have in your example.

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