Connecting Tech Pros Worldwide Forums | Help | Site Map

Converting ISO8601 format to Date

Jim Davis
Guest
 
Posts: n/a
#1: Aug 29 '05
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet
that will convert the common ISO8601 date formats to a JS date?

By "common" I mean at the least ones described in this W3C note:

http://www.w3.org/TR/NOTE-datetime

For my requirements the code would be need to be open sourceable under the
BSD license.

If not then my plan is to do a RegEx for each of the formats and attempt to
match from the most complex to the least. (I'm not searching through
arbitrary text but rather examining a single value that SHOULD be a date in
one of those formats) Sound like a plan?

Jim Davis



Dr John Stockton
Guest
 
Posts: n/a
#2: Aug 30 '05

re: Converting ISO8601 format to Date


JRS: In article <devios01t7t@news1.newsguy.com>, dated Mon, 29 Aug 2005
14:05:21, seen in news:comp.lang.javascript, Jim Davis
<newsmonkey@vboston.com> posted :[color=blue]
>Before I reinvent the wheel I thought I'd ask: anybody got a code snippet
>that will convert the common ISO8601 date formats to a JS date?
>
>By "common" I mean at the least ones described in this W3C note:
>
>http://www.w3.org/TR/NOTE-datetime[/color]

URLs should be encased in <...> or <URL:...> for optimum reader
satisfaction. However, you should remember that those who read News
off-line would prefer to see the information in the article itself.
[color=blue]
>For my requirements the code would be need to be open sourceable under the
>BSD license.[/color]

Bovine Spongiform Disencephaly?
[color=blue]
>If not then my plan is to do a RegEx for each of the formats and attempt to
>match from the most complex to the least. (I'm not searching through
>arbitrary text but rather examining a single value that SHOULD be a date in
>one of those formats) Sound like a plan?[/color]

The structure of the formats is, as I recall, rigid yyyy-mm-dd or yyyy-
Www-d or yyyy-ddd except that parts may be omitted. In that case, you
may only need the RegExps, as long as you are prepared to accept empty
parts. But note that ISO 8601:2000 allows some representations to be
used only by agreement.

Processing data in various ways until some form which might be correct
is tried is a very American thing to do, and usually leads to error in
the end. In a non-ISO context, success with Christmas Day does not
justify presumption of success with Guy Fawkes' Day.

Please read the newsgroup FAQ before asking questions. See below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jim Davis
Guest
 
Posts: n/a
#3: Aug 31 '05

re: Converting ISO8601 format to Date


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:bww6QyB83JFDFwvP@merlyn.demon.co.uk...[color=blue]
> JRS: In article <devios01t7t@news1.newsguy.com>, dated Mon, 29 Aug 2005
> 14:05:21, seen in news:comp.lang.javascript, Jim Davis
> <newsmonkey@vboston.com> posted :[color=green]
>>Before I reinvent the wheel I thought I'd ask: anybody got a code snippet
>>that will convert the common ISO8601 date formats to a JS date?
>>
>>By "common" I mean at the least ones described in this W3C note:
>>
>>http://www.w3.org/TR/NOTE-datetime[/color]
>
> URLs should be encased in <...> or <URL:...> for optimum reader
> satisfaction. However, you should remember that those who read News
> off-line would prefer to see the information in the article itself.[/color]

Point well taken.
[color=blue][color=green]
>>For my requirements the code would be need to be open sourceable under the
>>BSD license.[/color]
>
> Bovine Spongiform Disencephaly?[/color]

Well... in this case let's say that if you don't know then it doesn't matter
to you.
[color=blue][color=green]
>>If not then my plan is to do a RegEx for each of the formats and attempt
>>to
>>match from the most complex to the least. (I'm not searching through
>>arbitrary text but rather examining a single value that SHOULD be a date
>>in
>>one of those formats) Sound like a plan?[/color]
>
> The structure of the formats is, as I recall, rigid yyyy-mm-dd or yyyy-
> Www-d or yyyy-ddd except that parts may be omitted. In that case, you
> may only need the RegExps, as long as you are prepared to accept empty
> parts. But note that ISO 8601:2000 allows some representations to be
> used only by agreement.[/color]

I was able to address the problem with a coupla of RegExp's in the end. I
was hoping to be able to adopt something off the shelf, but finding nothing
went ahead and did it. Once it's been cleaned up I plan to release it to
community.

The purpose is not to represent a full 8601 parser (I'll leave that to much
bigger brains) - only to parse those (6) formats specified by the WC3 note.
[color=blue]
> Processing data in various ways until some form which might be correct
> is tried is a very American thing to do, and usually leads to error in
> the end. In a non-ISO context, success with Christmas Day does not
> justify presumption of success with Guy Fawkes' Day.[/color]

I really haven't the faintest idea what you're talking about here... ;^)

This is not a "fishing expedition" but rather a situation where arbitrary
dates may be sent. The six formats required cover a range of progressive
specificity and so the parser should understand them all.
[color=blue]
> Please read the newsgroup FAQ before asking questions. See below.[/color]

The FAQ doesn't seem to inform this question... is there something specific
in it you're suggesting I missed? Or do you just say this a lot? ;^)

Jim Davis


Jim Davis
Guest
 
Posts: n/a
#4: Aug 31 '05

re: Converting ISO8601 format to Date



Well... I think I've worked this out. I've appended the code below but if
anybody wants a copy let me know.

This handles (barring bugs!) the following subset of ISO datetime stamps:

YYYY (eg 1997)
YYYY-MM (eg 1997-07)
YYYY-MM-DD (eg 1997-07-16)
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20Z)
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30Z)
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45Z)

The code works well so far... but there are some caveats:

+) The RegExs at play here don't do much validation. For example while they
won't match a month of "14" they will match a month of "2" and a date of
"31". This will also only honors years from 1600 on (I've no desire to play
with Gregorian vrs Julian dates).

I've split out the expression fragments and maybe someday will add the
look-aheads required to do day of month validation... but I doubt I'll ever
bother with leap year validation. The rule here is "good enough for what I
need not perfect".

+) I've been leniant (and a little lazy) on the element separators. While
they are required you can use slashes, dashes, dots or colons. For
milliseconds you can use a dot or a comma. The ISO8601 standard is dashes
to separate date parts, colons to separate time parts and a dot (decimal) to
separate milliseconds.

+) If the input does not validate to a date a null is returned.

+) If there is no timezone information the date returned will be in local
time.

+) If the UTC indicator exists (a trailing "Z") then the date returned will
be in UTC time.

+) If timezone offset information exists it will be used to convert the
input to the local time.

If anybody finds any errors in this I'd much appreciate hearing about them!

Jim Davis


// Convert Dates from iso8601
function ISO8601ToDate(CurDate) {

// Set the fragment expressions
var S = "[\\-/:.]";
var Yr = "((?:1[6-9]|[2-9][0-9])[0-9]{2})";
var Mo = S + "((?:1[012])|(?:0[1-9])|[1-9])";
var Dy = S + "((?:3[01])|(?:[12][0-9])|(?:0[1-9])|[1-9])";
var Hr = "(2[0-4]|[01]?[0-9])";
var Mn = S + "([0-5]?[0-9])";
var Sd = "(?:" + S + "([0-5]?[0-9])(?:[.,]([0-9]+))?)?";
var TZ = "(?:(Z)|(?:([\+\-])(1[012]|[0]?[0-9])(?::?([0-5]?[0-9]))?))?";

// RegEx the input
// First check: Just date parts (month and day are optional)
// Second check: Full date plus time (seconds, milliseconds and TimeZone
info are optional)
var TF;
if ( TF = new RegExp("^" + Yr + "(?:" + Mo + "(?:" + Dy + ")?)?" +
"$").exec(CurDate) ) {} else if ( TF = new RegExp("^" + Yr + Mo + Dy + "T" +
Hr + Mn + Sd + TZ + "$").exec(CurDate) ) {};

// If the date couldn't be parsed, return null
if ( !TF ) {
return null;
};

// Default the Time Fragments if they're not present
if ( !TF[2] ) { TF[2] = 1 } else { TF[2] = TF[2] - 1 };
if ( !TF[3] ) { TF[3] = 1 };
if ( !TF[4] ) { TF[4] = 0 };
if ( !TF[5] ) { TF[5] = 0 };
if ( !TF[6] ) { TF[6] = 0 };
if ( !TF[7] ) { TF[7] = 0 };
if ( !TF[8] ) { TF[8] = null };
if ( TF[9] != "-" && TF[9] != "+" ) { TF[9] = null };
if ( !TF[10] ) { TF[10] = 0 } else { TF[10] = TF[9] + TF[10] };
if ( !TF[11] ) { TF[11] = 0 } else { TF[11] = TF[9] + TF[11] };

// If there's no timezone info the data is local time
if ( !TF[8] && !TF[9] ) {
return new Date(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]);
};
// If the UTC indicator is set the date is UTC
if ( TF[8] == "Z" ) {
return new Date(Date.UTC(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6],
TF[7]));
};
// If the date has a timezone offset
if ( TF[9] == "-" || TF[9] == "+" ) {
// Get current Timezone information
var CurTZ = new Date().getTimezoneOffset();
var CurTZh = TF[10] - ((CurTZ >= 0 ? "-" : "+") +
Math.floor(Math.abs(CurTZ) / 60))
var CurTZm = TF[11] - ((CurTZ >= 0 ? "-" : "+") + (Math.abs(CurTZ) % 60))
// Return the date
return new Date(TF[1], TF[2], TF[3], TF[4] - CurTZh, TF[5] - CurTZm,
TF[6], TF[7]);
};

};


Dr John Stockton
Guest
 
Posts: n/a
#5: Aug 31 '05

re: Converting ISO8601 format to Date


JRS: In article <df32pv010ap@news3.newsguy.com>, dated Tue, 30 Aug 2005
21:57:26, seen in news:comp.lang.javascript, Jim Davis
<newsmonkey@vboston.com> posted :
[color=blue]
>I was able to address the problem with a coupla of RegExp's in the end.[/color]

Please do not use slang. A substantial portion of this newsgroup are
not native users of English or American, and may have only learned the
proper forms of English.


[color=blue]
>The purpose is not to represent a full 8601 parser (I'll leave that to much
>bigger brains) - only to parse those (6) formats specified by the WC3 note.
>[color=green]
>> Processing data in various ways until some form which might be correct
>> is tried is a very American thing to do, and usually leads to error in
>> the end. In a non-ISO context, success with Christmas Day does not
>> justify presumption of success with Guy Fawkes' Day.[/color]
>
>I really haven't the faintest idea what you're talking about here... ;^)[/color]

Indeed - perhaps you are not well-informed about the history of pre-
colonial times.

[color=blue]
>This is not a "fishing expedition" but rather a situation where arbitrary
>dates may be sent. The six formats required cover a range of progressive
>specificity and so the parser should understand them all.[/color]

You still have not given those formats here.

[color=blue][color=green]
>> Please read the newsgroup FAQ before asking questions. See below.[/color]
>
>The FAQ doesn't seem to inform this question... is there something specific
>in it you're suggesting I missed? Or do you just say this a lot? ;^)[/color]


Well, you could try searching it for 'date' 'dates' 'time' or 'times'.
After all, it was you, I think, who did not want to re-invent the wheel.
You could also think more deeply about "See below".

Validating the values of the digits of a date by RegExp is a waste of
effort, since it can be done so much more easily by other means (and, as
has been shown by posters here in the past, it can be done rapidly by
deploying concentrated cunning). Time field value validation by RegExp
in not unreasonable, but not necessary either.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jim Davis
Guest
 
Posts: n/a
#6: Sep 1 '05

re: Converting ISO8601 format to Date


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:1YYxasCcuhFDFwJ3@merlyn.demon.co.uk...[color=blue]
> JRS: In article <df32pv010ap@news3.newsguy.com>, dated Tue, 30 Aug 2005
> 21:57:26, seen in news:comp.lang.javascript, Jim Davis
> <newsmonkey@vboston.com> posted :
>[color=green]
>>I was able to address the problem with a coupla of RegExp's in the end.[/color]
>
> Please do not use slang. A substantial portion of this newsgroup are
> not native users of English or American, and may have only learned the
> proper forms of English.[/color]

There's nothing here that actually makes one want to continue a conversation
with you.

I mean I can respect an asshole that knows he's an asshole and yet still
helps out... but you just truly like being an asshole, don't you?

Jim Davis


Randy Webb
Guest
 
Posts: n/a
#7: Sep 1 '05

re: Converting ISO8601 format to Date


Jim Davis said the following on 8/31/2005 9:01 PM:
[color=blue]
> "Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
> news:1YYxasCcuhFDFwJ3@merlyn.demon.co.uk...
>[color=green]
>>JRS: In article <df32pv010ap@news3.newsguy.com>, dated Tue, 30 Aug 2005
>>21:57:26, seen in news:comp.lang.javascript, Jim Davis
>><newsmonkey@vboston.com> posted :
>>
>>[color=darkred]
>>>I was able to address the problem with a coupla of RegExp's in the end.[/color]
>>
>>Please do not use slang. A substantial portion of this newsgroup are
>>not native users of English or American, and may have only learned the
>>proper forms of English.[/color]
>
>
> There's nothing here that actually makes one want to continue a conversation
> with you.
>
> I mean I can respect an asshole that knows he's an asshole and yet still
> helps out... but you just truly like being an asshole, don't you?
>[/color]

I see you have finally discovered what John Stockton is all about.....

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Closed Thread