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?

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless 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

Comments