Converting ISO8601 format to Date

J

Jim Davis

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 29 Aug 2005
14:05:21, seen in Jim Davis
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

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.
For my requirements the code would be need to be open sourceable under the
BSD license.

Bovine Spongiform Disencephaly?
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?

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

Jim Davis

Dr John Stockton said:
JRS: In article <[email protected]>, dated Mon, 29 Aug 2005
14:05:21, seen in Jim Davis


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.

Point well taken.
Bovine Spongiform Disencephaly?

Well... in this case let's say that if you don't know then it doesn't matter
to you.
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.

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

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.
Please read the newsgroup FAQ before asking questions. See below.

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
 
J

Jim Davis

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

};
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 30 Aug 2005
21:57:26, seen in Jim Davis
I was able to address the problem with a coupla of RegExp's in the end.

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.


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.


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

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

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.

You still have not given those formats here.

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? ;^)


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

Jim Davis

Dr John Stockton said:
JRS: In article <[email protected]>, dated Tue, 30 Aug 2005
21:57:26, seen in Jim Davis


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.

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
 
R

Randy Webb

Jim Davis said the following on 8/31/2005 9:01 PM:
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?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top