Opera's ISO Compatible Date Object
Posted by mattc at Dec 13, 06 04:36 PM ... Comments (0)
I was just about to write a regular expression, when suddenly ...
I stumbled on the fact that upon feeding dates formatted as RFC 822 (as commonly found in RSS 2.0) in to a newly instantiated Javascript Date object it just handles it. No ifs or buts, it just works. I didn't expect that.
var foo = 'Fri, 04 Apr 2003 05:04:39 GMT';
var bar = new Date( foo );
var woo = bar.getYear() // woo holds '2003'
How very helpful. This means I could combine some getElementsByTagName construct with Date to give me an array of feed items by date without too much fuss ...
var foo = new Array();
// where 'o' is the response from some xmlHTTP request
var rss = o.responseXML.getElementsByTagName("item");
for ( var j = 0; j < rss.length; j++ ) {
foo.push( new Date( rss[j].getElementsByTagName("pubDate")[0].textContent ) );
}
But wait. Simon and Mark point out that RSS has many dates and times.
So I wonder how JavaScript handles these?
// load each date type in to foo var foo = new Array('2003-03-21T16:28:40', '2003-04-03T07:45:57-08:00', 'Fri, 04 Apr 2003 05:04:39 GMT', 'Fri, 28 Mar 2003 05:18:59 -0800', '1049379042.0', '2003-03-21T16:28:40', '2003-01-17T13:03:00+00:00', '2003-03-27T19:41:49-06:00' ); // iterate foo and write the year to the screen for ( var i = 0; i < foo.length; i++ ) { var bar = new Date( foo[i] ); // print output, attempt to call getFullYear document.write( foo[i] + " - " + bar.getFullYear() + "\n" ); }
In Opera 9, almost perfectly ...
2003-03-21T16:28:40 - 2003
2003-04-03T07:45:57-08:00 - 2003
Fri, 04 Apr 2003 05:04:39 GMT - 2003
Fri, 28 Mar 2003 05:18:59 -0800 - 2003
1049379042.0 - NaN // bah!
2003-03-21T16:28:40 - 2003
2003-01-17T13:03:00+00:00 - 2003
2003-03-27T19:41:49-06:00 - 2003
The only error is the obscure '1049379042.0', which I assume is a reference to the number of seconds passed since midnight 1970. I'm not sure who is using that in their pubDate fields !?
IE 6, Firefox 1.5 & Safari 2.0.4 do much worse, only managing to parse and return valid Date objects from 2 out of the 7 dates.
2003-03-21T16:28:40 - NaN 2003-04-03T07:45:57-08:00 - NaN Fri, 04 Apr 2003 05:04:39 GMT - 2003 Fri, 28 Mar 2003 05:18:59 -0800 - 2003 1049379042 - NaN 2003-03-21T16:28:40 - NaN 2003-01-17T13:03:00+00:00 - NaN 2003-03-27T19:41:49-06:00 - NaN
So, to recap, Opera's Date object supports ISO 8601 date parsing upon construction, everything else doesn't.
I find the ECMA standard terse at the best of times, it's unclear whether it's meant to be doing this or not.
I also see MochiKit provides extensions for this sort of thing via it's DateTime library.
Comments (0)
Post Your Comments
random bookmark
link summary
month
October 2009 (1)
September 2009 (14)
August 2009 (16)
July 2009 (21)
June 2009 (24)
May 2009 (16)
April 2009 (2)
March 2009 (22)
February 2009 (11)
January 2009 (11)
December 2008 (9)
November 2008 (16)
October 2008 (18)
September 2008 (11)
August 2008 (12)
July 2008 (20)
June 2008 (15)
May 2008 (27)
April 2008 (9)
March 2008 (10)
February 2008 (8)
January 2008 (8)
December 2007 (12)
November 2007 (10)
October 2007 (10)
September 2007 (6)
August 2007 (13)
July 2007 (8)
June 2007 (10)
May 2007 (12)
April 2007 (5)
March 2007 (12)
February 2007 (13)
January 2007 (22)
December 2006 (21)
November 2006 (28)
August 2006 (1)
category
code (15)
food (4)
notes (4)
photo (18)
project (2)
quote (12)
sketch (13)
soup (10)
travel (2)
matt chadburn . wiki . 2010