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!
Both comments and pings are currently closed.
