While making some improvements to my ilist metadata (iTunes MPEG4 style metadata) classes I ended up needing to convert an ISO 8601 date and time string to a native AS3 Date object and vice versa.
An ISO 8601 date and time string comes in two flavors, basic and extended. As you can see below, the basic format simply eschews dashes and colons in favor of a more compact syntax.
The standard requires a fixed number of digits to represent a date (8 digits) or a time (6 digits). When fewer digits are needed to represent a date / time the number must be padded with leading zeros. The Z at the end of the string designates the UTC “Zulu” timezone (GMT or so called universal time).
I took a quick buzz around the interwebs, but couldn’t find an AS3 lib that could convert one of these strings into a date object or take a date object and have it generate a correctly formatted string, so, you guessed it–I spent my Sunday evening whipping together a utility class that will do the trick (download the source here).
I’m not claiming compliance with every nook and cranny of the spec, but it covers the fundamentals decently. Hand the util an AS3 Date object and you can generate a date, time or date + time in the basic or extended format. You can also parse a basic / extended date, time or date + time string and have an AS3 Date object returned with the appropriate UTC values set on it.
Here’s what the basic usage would look like for a date + time scenario:
// instantiate a ISO8601Util object var util:ISO8601Util = new ISO8601Util(); // parse a date + time string into an AS3 Date - takes either a basic or // extended representation var date:Date = util.parseDateTimeString( "2009-02-21T09:07:59Z" ); // to turn a date into an ISO 8601 date + time representation grab a date object var currentdate:Date = new Date(); // create a string with the extended or basic format var extended:String = util.formatExtendedDateTime( currentdate ) var basic:String = util.formatBasicDateTime( currentdate );
Hopefully, I’m now the only poor bastard who has to do this!
трейдеров Ñ„Ð¾Ñ€ÐµÐºÑ Ñ‚Ñ€ÐµÐ¹Ð´ÐµÑ€Ð¾Ð² Ñ„Ð¾Ñ€ÐµÐºÑ http://investitovanie.ru заработать форекÑ
Thanks, you saved me bunches of time. I owe you a beer!
Looking for a solution to the same problem, yours was close but I also needed to parse time zone information. Turns out Flex has the DateUtil class that will do this for us!
http://stackoverflow.com/questions/691342/how-to-parse-an-iso-formatted-date-in-flex-as3
Thanks a ton.. I owe you a beer too..
Your class just worked awesome , saved hours..
you’ve saved me alot of time aswell… many thanks. I was working on a myspace app and couldn’t get the date formatting nicely.
rock on, thanks!
Thanks for this class! Works great.
Thanks – saved me some time as well. As a note, I converted all functions/consts to static, so I can call the conversions w/o instantiating the class.
Great work and many thanks. You saved me loads of time aswell.
I was looking for something to convert mysql datetime fields which are formatted like so:
2009-11-24 11:11:10
Very similar to the extended pattern, so I modified the parseDateTime function to handle this as well:
#replace with a space (and trim if you want)
val = val.replace(/-|:|T|Z/g, ” “);
#stays the same
var date:Date = parseBasicDate(val.substr(0, 8));
#start at 9, not 8
date = parseBasicTime(val.substr(9, 6), date);
Thanks for the util.
That last post was posted before it was complete damnit!
Obviously if you replace those delimiters with a space then the 0,8 and 9,6 boundries will no longer work, so you can just change the boundries.
However, better solution is to simply remove any white space from val as well:
val = val.replace(/-|:|T|Z|\s/g, ”);
Then everything else works as is.
Hi,
works like a charm, thanks! I only set all functions to static, that way you save the line for creating an object that actually has no need to be created, I personally like that better.
Cheers!
Excellent! This saved me a good bit of time – thanks!
That saved me some time while parsing NewsML date – thanks a lot!
Great, thanks!
Thanks a ton, man!
There is already a class that handles this written by adobe:
http://code.google.com/p/as3corelib/source/browse/trunk/src/com/adobe/utils/DateUtil.as
Just copy the method called “parseW3CDTF” from there which can be used in flash (flex not required if you don’t need all the other methods in that class)
how to convert from time 15h00 to iso8601
and how to get T in between date and time 2011-02-26 04:00
You rock! Thanks so much!
Thanks, I was looking for something like this, It worked great with the Facebook Graph API.