Home » Odeon Blogs » Horia, Coding Passion »

Major Headache With Flex AS3 Date Formatting

Major Headache With Flex AS3 Date Formatting

Date formatting is one of my favorite features of coding for the web. I've always pushed its limits, especially in my PHP days when I would include words and even function calls, sometimes html tags, right in the string format. It's a lot of fun.

 What's not so much fun is the slight difference between format conventions. PHP date format is not like Python date format which is not like Django date format and so on. Most of the time, though, the basics are intuitive. But then there's Actionscript.

  1. var date = new Date( 2010, 1, 1 ); // let's set the date to the 1st of January, 2010
  2. var df:DateFormatter = new DateFormatter();
  3. df.formatString = 'YYYY-MM-DD HH:NN';
  4. df.format( date ); //returns '2010-02-01 24:00'
  5. // wait, what?


For some reason, the people that handled this part of the language thought it was a good idea to have the days start at one, but have the months start at zero!
I've gotten used to that now, and I have libs that take care of that. Still, it's a little confusing; especially to newcomers.


Now, the other problem is a bit more subtle. We all know that there's no such thing as the hour 24. Because, even in real life, hours are 0 based. But, for some reason, the people that handled this part of the language thought it was a good idea to have the 'H' in the formatting be 1 based. Oh, it's great for when you want to know which nth hour it is.

Because that's the way we speak, right?

"Jack, what time is it?" "It's fourteen minutes past the eleventh hour, Jill."

 The correct symbol in the format string is 'J' - the zero based hour representation. Why?!
But if we run into this error issue, this is our fault. We assume people are sane, and H would stand for hour, as with other languages. Properly consulting the documentation will reveal this little feature. So we're actually dealing with a funny case of RTFM( Read The Flex Manual ).

 Here's the code, adjusted to cater Actionscript's trifles.

  1. var date = new Date( 2010, 1 - 1, 1 ); // let's properly set the date to the 1st of January, 2010
  2. var df:DateFormatter = new DateFormatter();
  3. df.formatString = 'YYYY-MM-DD JJ:NN';
  4. df.format( date ); //returns '2010-01-01 00:00'
  5. // there we go!



Leave a Comment :

(required)


(required)




(required)




(required)





Page generated in: 0.19s