Flex date gotcha

This surprised me. I was working with formatting a date value returned from ColdFusion. The dates looked like so:

Thu Sep 28 17:34:21 GMT-0500 2006

I used a dateFormatter with a format string of M/D/YYYY. Something odd happened to the year value though. My years were all 500. I'm pretty sure there were no users for my application around back then.

Turns out the problem was in how I was passing the date to the dateFormatter. This was my first draft:

private function dateLabelFunction(item:Object, column:DataGridColumn):String {
   var theValue:String = item[column.dataField];
   return myDateFormatter.format(theValue);
}

It seems like the dateFormatter object doesn't quite grok the GMT portion of the date. Which seems odd - I mean - it's not like that isn't a known format. What makes it even more odd is that when I switched to creating a date object first, and then formatting it, it worked just fine:

private function dateLabelFunction(item:Object, column:DataGridColumn):String {
   var theValue:String = item[column.dataField];
   var theDate:Date = new Date(theValue);
   return myDateFormatter.format(theDate);
}

So the Date constructor had no issue with the string, but the formatter did not. I would imagine they would have used the same core code?

Comments

Hmm... good tip! I am in the habit of formatting the date within the sql query in the cfc (i.e. select date_format(myDate,'%c/%e/%Y') as myFormattedDate from myTable) ... any pro's or con's either way?
# Posted By Jen | 1/22/07 1:25 PM
I normally say leave the data alone and let the client make it pretty. But I've done what you have done for AJAX apps as I've not found a nice way to format in JavaScript. (Well, format is ok, but parsing strings to dates is a pain for me.)
# Posted By Raymond Camden | 1/22/07 1:36 PM
I have an Urgent need for a Perm. Sr. Applications Developer/Flex Architect in the Dallas Texas area, would you happen to know of anyone with that experience that would be interested? If so please let me know.

Kim Dobson
Placement Manager
214-277-2097
# Posted By Kim Dobson | 1/22/07 2:26 PM
Wish I could help you - although I'm definitely not a Senior Flex person yet. :)
# Posted By Raymond Camden | 1/22/07 2:32 PM
The Flex DateFormatter is written in ActionScript, and the Flash Date class is built-in (written in c or c++, I suppose). Hopefully, someone on the Flex team will ask to see the Flash parsing code for a future release. :)
# Posted By Josh | 1/22/07 5:32 PM
I'm creating a class with a Date field, and this helped. Doesn't make sense. This works to create a Date from XML:

if (x.expiration != null) {
var s: String = x.expiration;
_expiration = new Date(s);
}

This doesn't:

if (x.expiration != null) {
_expiration = new Date(x.expiration);
}
# Posted By K | 2/21/07 4:43 PM
Thanks for the tip! I've just run through this gotcha. I hope Adobe would fix soon.
# Posted By Berryblitz | 9/23/07 11:40 PM
How exactly do you format a date in a DataGrid?
I did not see those columns to have a property for a format function.
# Posted By Rosen | 1/23/08 2:21 PM
Well you apply a labelFormat for the column, and which calls a function that can make use of a DateFormatter.
# Posted By Raymond Camden | 1/23/08 3:29 PM
Raymond means you should use labelFunction (not labelFormat) to call a custom function that can format your date.
# Posted By Steve | 3/27/08 7:24 AM
This has been fixed, coming soon to a Flex SDK release near you.

https://bugs.adobe.com/jira/browse/SDK-13650
# Posted By Steven Erat | 7/3/08 12:39 PM