Flex DateField and formats

ugh, what a waste of time on this today. SO, you have a DateField control in your fancy uber high UX’d out BRIA form when the business comes to you and says hey, we want the date formated as: DD/MM/YYYY. Sure says you , cake right? So, you precede to the docs on DateField and see a formatString property that accepts a string the same as DateFormatter.formatString. Yippee! NOT! Using the formatString of DD/MMM/YYYY actually formats to ’12/02022008′, oops. Ok, so you bust out the labelFunction and bamm! Works a treat. Until you programmatically try setting selectedDate on this DateField that is. You have to set the parseFuntion to null to keep Flex from messing with it and your head. Example code below.

Peas

DK

<mx:Script>
<![CDATA[
private function doDateLabel(item:Date):String {
return dateFormatter.format(item);
}
]]>
</mx:Script>
<mx:DateFormatter id="dateFormatter" formatString="DD/MMM/YYYY" />
<mx:DateField id="begin"
showToday="true"
labelFunction="doDateLabel"
parseFunction="null"/>

This entry was posted in AIR, Flex, Universal Mind. Bookmark the permalink.

13 Responses to Flex DateField and formats

  1. Tim Wilson says:

    Thanks, I hit this problem last week, and couldn’t believe it (and yes, it did mess with my head too!). I got as far as working out I needed to do something with the parseFunction, then I ended up leaving it for another day.

    If I can just set that to null, then you’ve saved me a lot of time.

    Thanks again.

  2. Pramod Rao says:

    Hey thanks dude…. I literally banged my head yesterday for this and it all boiled down to this one single line…

  3. flexo says:

    Thanks for such a nice example. God bless you

  4. Craig Jarman says:

    Absolutely brilliant. I’d given up on this a year ago….

  5. Adrian says:

    I was looking for this info, brain freeze, thank you.

    Any difference in just doing the following:

    I am getting the same results but I didn’t know if there might be another advantage to going through the process you showed.

  6. Adrian says:

    I meant to show this string, sorry

    mx:DateField id=”issDateField” showToday=”true” formatString=”MM-DD-YYYY”

  7. rich says:

    I share your disgust that this little trick is even necessary. Thanks for saving me and my team a lot of pain.

  8. Maddy says:

    Thanks dude!!

  9. Allan says:

    Any follow-up to tim’s message above? I get the same thing. Thanks.

  10. JabbyPanda says:

    Finally, there is a bug in JIRA for this issue and it waits for your votes!

    https://bugs.adobe.com/jira/browse/SDK-25343

    Beware, resetting “parseFunction” to null works only for non-editable DateFields.

    Better workaround for the time being would be to set “formatString” on DateField to the same value as “formatString” is set for DateFormatter.

    e.g.

  11. JabbyPanda says:

    sample code was eaten by html worm, resubmitting

    <mx:DateField id=”begin”
    showToday=”true”
    labelFunction=”doDateLabel”
    parseFunction=”null”/>

  12. JabbyPanda says:

    and one more time, this time correct sample code:

    <mx:DateField id=”begin”
    showToday=”true”
    labelFunction=”doDateLabel”
    formatString=”DD/MMM/YYYY”/>