- mx.formatters.DateFormatter
- spark.formatters.DateTimeFormatter
- flash.globalization.DateTimeFormatter
I use the latter one because he supports locale. But more about this in a moment. First let's use this Formatter to convert Date to a custom string:
var date:Date = new Date(); var formatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT); formatter.setDateTimePattern("dd.MM.yyyy"); var dateString:String = formatter.format(date)
So now in dateString object you have date string that matches pattern set in setDateTimePattern() function (mind the small "d" and "y" letters). It's very nice feature but we go a little further and use locale instead of a custom pattern. In order to do that we need to change above code a little bit:
var date:Date = new Date(); var formatter:DateTimeFormatter = new DateTimeFormatter("pl_PL"); formatter.setDateTimeStyles(DateTimeStyle.SHORT, DateTimeStyle.NONE); var dateString:String = formatter.format(date)
Two things are changed, instead of using LocaleID.DEFAULT I wrote Polish locale, so date would be formatted according to Polish rules. Great idead is to put some kind of global variable there and change date formatting when user changes his locale. Of course you can leave LocaleID.DEFAULT there, wchich should work fine in most cases. Second change is use setDateTimeStyles() function instead of setDateTimePattern(). Basically this function change the way that date and time (for each of them this is set separately) is converted, your options are LONG, MEDIUM, SHORT and even NONE.
No comments:
Post a Comment