Parsing dates with timezones in Flex

In a previous example, “Parsing ISO dates with Flex and ActionScript”, we saw how to parse ISO formatted dates (2008-01-25T06:14:23Z) and convert them into an ActionScript 3.0 Date object.

The following example shows how you can convert a Java formatted date (Wed Jul 22 14:29:30 PST 2009) and convert the timezone into an ActionScript 3.0 friendly format (UTC-0700) so you can use the static Date.parse() method to convert them into an ActionScript 3.0 Date object.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<mx:Application name="Date_parse_test"
            private const datStr1:String = "Wed Jul 22 14:29:30 PST 2009";
            private const datStr2:String = "Wed Jul 22 14:29:30 EDT 2009";
            private var timeZones:Array = [
                /* Hawaii-Aleutian Standard/Daylight Time */
                {abbr:"HAST", zone:"UTC-1000"},
                {abbr:"HADT", zone:"UTC-0900"},
                /* Alaska Standard/Daylight Time */
                {abbr:"AKST", zone:"UTC-0900"},
                {abbr:"ASDT", zone:"UTC-0800"},
                /* Pacific Standard/Daylight Time */
                {abbr:"PST", zone:"UTC-0800"},
                {abbr:"PDT", zone:"UTC-0700"},
                /* Mountain Standard/Daylight Time */
                {abbr:"MST", zone:"UTC-0700"},
                {abbr:"MDT", zone:"UTC-0600"},
                /* Central Standard/Daylight Time */
                {abbr:"CST", zone:"UTC-0600"},
                {abbr:"CDT", zone:"UTC-0500"},
                /* Eastern Standard/Daylight Time */
                {abbr:"EST", zone:"UTC-0500"},
                {abbr:"EDT", zone:"UTC-0400"},
                /* Atlantic Standard/Daylight Time */
                {abbr:"AST", zone:"UTC-0400"},
                {abbr:"ADT", zone:"UTC-0300"},
                /* Newfoundland Standard/Daylight Time */
                {abbr:"NST", zone:"UTC-0330"},
                {abbr:"NDT", zone:"UTC-0230"}
            private function simpleDateParser(value:String):Date {
                var dat:String = value;
                if (isNaN(Date.parse(dat))) {
                    var idx:uint;
                    var len:uint = timeZones.length
                    var obj:Object;
                    for (idx=0; idx<len; idx++) {
                        obj = timeZones[idx];
                        if (dat.indexOf(obj.abbr) > -1) {
                            dat = dat.replace(obj.abbr,;
                            return new Date(Date.parse(dat));
                // TimeZone not found, or conversion not needed.
                return new Date(Date.parse(dat));
        <mx:Label text="{datStr1}" fontWeight="bold" />
        <mx:Label initialize="event.currentTarget.text = simpleDateParser(datStr1);" />
        <mx:HRule width="100%" />
        <mx:Label text="{datStr2}" fontWeight="bold" />
        <mx:Label initialize="event.currentTarget.text = simpleDateParser(datStr2);" />

2 thoughts on “Parsing dates with timezones in Flex

  1. This example converting any timezone to default system timezone. Is this possible to convert any timezone to any timezone?

Comments are closed.