Converting between dates and strings using the DateField class in Flex

by Peter deHaan on December 15, 2007

in DateField

The following example shows how you can convert Date objects to String objects using the static DateField.dateToString() method in Flex. As an added bonus, the example also shows how you can convert String objects to Date Objects using the static DateField.stringToDate() method.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/12/15/converting-between-dates-and-strings-using-the-datefield-class-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.controls.DateField;

            private const MASK:String = "DD/MM/YYYY";

            private function init():void {
                var today:Date = new Date();
                var halloween:String = "31/10/2007";

                var todayString:String = DateField.dateToString(today, "DD/MM/YYYY");
                todayLabel.text = todayString;

                var halloweenDate:Date = DateField.stringToDate(halloween, "DD/MM/YYYY");
                halloweenLabel.text = halloweenDate.toDateString();
            }
        ]]>
    </mx:Script>

    <mx:Form>
        <mx:FormItem label="today ({MASK}):">
            <mx:Label id="todayLabel" />
        </mx:FormItem>
        <mx:FormItem label="Halloween:">
            <mx:Label id="halloweenLabel" />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.

{ 1 comment… read it below or add one }

1 Reny February 1, 2010 at 3:32 am

Thanks.. ^_______^

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: