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?
Archived 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?
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.)
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
Wish I could help you - although I'm definitely not a Senior Flex person yet. :)
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. :)
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);
}
Thanks for the tip! I've just run through this gotcha. I hope Adobe would fix soon.
How exactly do you format a date in a DataGrid?
I did not see those columns to have a property for a format function.
Well you apply a labelFormat for the column, and which calls a function that can make use of a DateFormatter.
Raymond means you should use labelFunction (not labelFormat) to call a custom function that can format your date.
This has been fixed, coming soon to a Flex SDK release near you.
https://bugs.adobe.com/jira...
Actually, the bug has NOT been fixed yet (as seen in the link above). Hopefully soon....?