Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp Parsing ISO-8601

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.

like image 735
mplungjan Avatar asked Nov 25 '11 12:11

mplungjan


People also ask

How do I read an ISO 8601 file?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).

Is ISO 8601 always UTC?

Date.prototype.toISOString() The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

What is ISO 8601 format SAS?

The SAS ISO 8601 formats for UTC with a time zone offset are based on the following time, datetime, and time zone offsets: the zero meridian time or datetime near Greenwich, England (The offset is always +|–0000 or +|–00:00.)

What time zone is ISO 8601?

Universal Coordinate Time is the time at the zero meridian, near Greenwich, England. UTC is a datetime value that uses the ISO 8601 basic form yyyymmddT hhmmss+|– hhmm or the ISO 8601 extended form yyyy-mm-ddT hh:mm:ss+|– hh:mm.


2 Answers

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

like image 152
Qtax Avatar answered Nov 13 '22 14:11

Qtax


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.

like image 22
Blem Avatar answered Nov 13 '22 14:11

Blem