Wednesday, May 11, 2011

Flex Date Problem

If you don't already know than there is a difference between American and British date formats
American is (Or American preferred Date format)mm/dd/yyyy (like 9/11/2003 or 9.1.2003)
While the British preferred date format isdd/mm/yyyy (like 11/9/2003 or 11.9.2003)

In Flex when you are creating a Date object by passing it the date String and if mistakenly you pass it date in British format it might show you unpredictable results, below is an example
Alert.show(new Date('15/11/2011').toDateString());will give you following date
Sun Mar 11 2012Of course this is not the date you wanted, and this is because flex was expecting data to be provided as mm/dd/yyyy but the data you provided was dd/mm/yyyy and surprisingly in this case flex will convert it into a wrong date.
So be carefull when you are supplying dates to Date class of action script and always make sure the date you are passing to it is in mm/dd/yyyy otherwise you will see unexpected results.

To understand my point here is another example
7/12/06, in American format , is July 12th, 2006 = 12 July 2006
7/12/06, In British format, is 7th December 2006 = 7 December 2006

No comments:

Post a Comment