I’ve been working with some Adobe Flex recently, and was having some trouble formatting dates. For those who don’t know, here (in th UK) we use the format “dd/mm/yyyy” (Day/Month/Year) as opposed to people over in the States using “mm/dd/yyyy” (Month/Day/Year).
I had to use the DateField component, and couldn’t find a way in heaven to get the date it returns formatted as “dd/mm/yyyy”.
I consider myself a flex beginner, and haven’t really done much with it (I know, shame on me), and usually have research for online resources (A.K.A “Google Desperation”).
Surprisingly, it’s not very easy to find good online resources for Flex, and when searching for it, I was mostly presented with dodgy date strings being split up and used as arrays.
Damn it! Isn’t there anything like Coldfusion’s DateFormat?
Basically what you have to to, is use mx:DateFormatter, and create your mask as such:
<mx:DateFormatter id="formatDate" formatString="DD/MM/YYYY" />
You can now use this formatter in order to format any date you might have as a string:
<mx:Script> <!--[CDATA[ public var myDate:Date = new Date; trace(formatDate.format(myDate)); ]]--> </mx:Script>
And this will format your date as “dd/mm/yyyy”. I hope this is useful to somebody else some other time.