Another basic little example showing how to format the text in a DateField control using the DateFormatter and the DateField control’s labelFunction property. This entry also shows the pretty basic trick of creating a separate ActionScript class file that holds the day names as constants so you don’t constantly have to think whether Monday is day 0 or day 1 (hint: it is day 1).

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function doLabel(item:Date):String {
                return dateFormatter.format(item);
            }
        ]]>
    </mx:Script>

    <mx:DateFormatter id="dateFormatter" formatString="MMM D, YYYY"/>

    <mx:DateField todayColor="red" labelFunction="doLabel" firstDayOfWeek="{Days.MONDAY}" />

</mx:Application>

Days.as

// ActionScript file
package {
    public class Days {
        public static const SUNDAY:uint = 0;
        public static const MONDAY:uint = 1;
        public static const TUESDAY:uint = 2;
        public static const WEDNESDAY:uint = 3;
        public static const THURSDAY:uint = 4;
        public static const FRIDAY:uint = 5;
        public static const SATURDAY:uint = 6;
    }
}

View source enabled in the following example

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Formatting dates using the DateFormatter

  1. Searching for this for some time now – i guess luck is more advanced than search engines

  2. vaibhav says:

    Hi
    i used DateFormatter and DateField control, using this i’m able to format the dates but Legends i’m using in the Graph are now depend on the locale that i specify first in the Flex Build path.

    If i remove DateFormatter and labelFunction attribute from the DateField it shows proper lcoale specific legends for the Graph?

    any reason why is this behavior?

    Thanks in advance,
    Vaibhav

Leave a Reply

Your email address will not be published.

You may 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