5

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.

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 4
    What have you tried? This site should not be to get free work done for you. – Evert Nov 25 '11 at 12:47
  • 1
    +1: must say this is an unnecessary down vote! – naveen Nov 25 '11 at 12:51
  • 4
    @Evert Seems like a reasonable question to me. He's a 10k user with hundreds of answers, so I think your comment is a bit unfair. – Michael Low Nov 25 '11 at 12:54
  • 10K doesn't mean he won't make a mistake :) Every time I open the unanswered page, there's a lot of 'write a regex for me'-type of questions. I tend to downvote those. – Evert Nov 25 '11 at 13:01
  • 4
    @Evert - what have I tried? Trying to help another SO user, I obviously have a) found the code that almost worked, b) created a fiddle and changed it to handle Firefox, c) figured out the regex for IE and Safari did not work on that particular string and d) asked SO community for help in ADDING 4 more chars to the regex. If you do not feel like helping, fine - feel free to IGNORE this question. If I knew perfectly how to do it, I would not ask for help! – mplungjan Nov 25 '11 at 13:12
  • @mplungjan, `2011-11-24T09:00:27+002` is not valid according to the referred standard. The time offset should be 2 or 4 digits, not 3. What does `+002` mean, `+02` or `+0020` (`+00:20`)? I'm guessing the first one. ;) – Qtax Nov 25 '11 at 13:38
  • Thanks @Qtax - Must have been a typo. Thanks for pointing it out – mplungjan Nov 25 '11 at 13:57
  • @mplungjan, if it's a typo remove the `\d?` in my answer. – Qtax Nov 25 '11 at 14:08

2 Answers2

13

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).

Qtax
  • 33,241
  • 9
  • 83
  • 121
2

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.

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
Blem
  • 796
  • 16
  • 36