Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an ISO formatted date in Flex (AS3)?

How can I parse an ISO date string into a date object in Flex (AS3)?

e.g.
2009-12-08T04:23:23Z
2009-12-08T04:23:23.342-04:00
etc...

like image 559
Chadwick Avatar asked Mar 27 '09 20:03

Chadwick


2 Answers

import com.adobe.utils.DateUtil;

var dateString:String = "2009-03-27T16:28:22.540-04:00";
var d:Date = DateUtil.parseW3CDTF(dateString);
trace(d);
var s:String = DateUtil.toW3CDTF(d);
trace(s);
[trace] Fri Mar 27 16:28:22 GMT-0400 2009
[trace] 2009-03-27T20:28:22-00:00

Turns out DateUtil handles everything in the W3C Date and Time spec. AS3 Dates do not maintain milliseconds, but they'll just be dropped if available.

Note that the W3C output is converted to UTC (aka GMT, or Zulu time).

like image 111
Chadwick Avatar answered Nov 02 '22 18:11

Chadwick


Example function to convert ISO into Date format

    public function isoToDate(value:String):Date 
    {
        var dateStr:String = value;
        dateStr = dateStr.replace(/\-/g, "/");
        dateStr = dateStr.replace("T", " ");
        dateStr = dateStr.replace("Z", " GMT-0000");

        return new Date(Date.parse(dateStr));
    }
like image 21
ShelS Avatar answered Nov 02 '22 16:11

ShelS