Flex date gotcha

This post is more than 2 years old.

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?

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Jen posted on 1/23/2007 at 12:25 AM

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?

Comment 2 by Raymond Camden posted on 1/23/2007 at 12:36 AM

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.)

Comment 3 by Kim Dobson posted on 1/23/2007 at 1:26 AM

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

Comment 4 by Raymond Camden posted on 1/23/2007 at 1:32 AM

Wish I could help you - although I'm definitely not a Senior Flex person yet. :)

Comment 5 by Josh posted on 1/23/2007 at 4:32 AM

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. :)

Comment 6 by K posted on 2/22/2007 at 3:43 AM

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);

Comment 7 by Berryblitz posted on 9/24/2007 at 8:40 AM

Thanks for the tip! I've just run through this gotcha. I hope Adobe would fix soon.

Comment 8 by Rosen posted on 1/24/2008 at 1:21 AM

How exactly do you format a date in a DataGrid?
I did not see those columns to have a property for a format function.

Comment 9 by Raymond Camden posted on 1/24/2008 at 2:29 AM

Well you apply a labelFormat for the column, and which calls a function that can make use of a DateFormatter.

Comment 10 by Steve posted on 3/27/2008 at 4:24 PM

Raymond means you should use labelFunction (not labelFormat) to call a custom function that can format your date.

Comment 11 by Steven Erat posted on 7/3/2008 at 9:39 PM

This has been fixed, coming soon to a Flex SDK release near you.


Comment 12 by Kai Griffin posted on 4/10/2009 at 10:24 PM

Actually, the bug has NOT been fixed yet (as seen in the link above). Hopefully soon....?