QuickDateFormatter – efficient date formatting in AS3
I've known that AS3 included some formatter classes, including DateFormatter, for some time now but didn't have cause to use them until today. I'm not sure if its laziness or that I just don't understand some level of abstraction Adobe was striving for with their implementation, but I found it maddeningly complicated to use. I was expecting something like php's date(), which usually looks something like this:
Well, you can imagine my horror (exaggerate much?) when I discovered that the same functionality in AS3 looked like this:
df.formatString = "MM/DD/YYYY";
var formattedDate:String = df.format(myUnformattedString);
My solution? QuickDateFormatter, whose usage looks like this:
Much quicker and cleaner, specially made for us lazy folk. I suppose to match the robustness of the built-in DateFormatter I would need to add some error handling but I am not really worried about that. This is meant for cases where you know the input is a valid date but you need to alter the formatting. You can grab the source here or simply copy and paste from below.
{
import mx.formatters.DateFormatter;
public class QuickDateFormatter
{
public static function format(str_dateString:String, str_dateFormat:String):String
{
var f:DateFormatter = new DateFormatter();
f.formatString = str_dateFormat;
return f.format(str_dateString);
}
}
}
Enjoy!
You can leave a response, or trackback from your own site.

Could you make it slightly more efficent by making ‘f’ private and initialising it in the constructor, leaving just format() to call during the format() method ?
If format() weren’t a static method then yes, you could. Since it is, however, the constructor never gets called and f would be undefined.
How would you format the date of a remote object cfc result when the date field coming back is being assigned to a datagrid field with the dataProvider?
I tried this and the compiler does not recognize “Version_Date”.
Nice site, BTW.
Thanks,
Jeff
Hi Jeff, thanks. I’m not sure exactly what you mean and I have actually never touched CF so I couldn’t really say. Something to keep in mind though is that the function expects two string parameters and also returns a string. There is currently no functionality to support an object being passed in if that is what you were looking for.
I tried this but the date that was returned is 02/17/500 and the input date was Sat Feb 17 00:00:00 GMT-0500 2007. Not sure what is happening.
I figured out the problem. I was passing in a date field from a CFC but the str_dateString was defined as a string. I changed it to object and it worked fine. This should work for Jeff.
Jeff write the following code it will slove your problem
in the action script file write the function “showDate”
private function showDate(item:Object, column:DataGridColumn):String
{
var field:String = column.dataField;
if (dateFormat == null) {
dateFormat = new DateFormatter();
}
// update the dateFormat string
dateFormat.formatString = “MM/DD/YYYY”; // displays am pm, hours 1-12, illustrative only } else if (displayTimeFormat == “24hour”) {
dateFormat.formatString = “MM/DD/YYYY”;
return dateFormat.format(item[field]);
}
// change only the datafield property of the datagrid
sorry Jeff mxml part is missing in the above code write the following code it will slove your problem
in the action script file write the function “showDate”
private function showDate(item:Object, column:DataGridColumn):String
{
var field:String = column.dataField;
if (dateFormat == null) {
dateFormat = new DateFormatter();
}
// update the dateFormat string
dateFormat.formatString = “MM/DD/YYYY”; // displays am pm, hours 1-12, illustrative only } else if (displayTimeFormat == “24hour”) {
dateFormat.formatString = “MM/DD/YYYY”;
return dateFormat.format(item[field]);
}
// change only the datafield property of the datagrid
sorry jeff mxml is here
“I’ve known that AS3 included some formatter classes, including DateFormatter”
Just to clarify, AS3 does not include any formatter classes, these are part of the Flex library (I’m inferring, but you don’t actually mention Flex.) I don’t mean to sound pedantic but since this post appeared at the top of “AS3 date formatter” I thought I would mention it.