return undefined;


QuickDateFormatter - efficient date formatting in AS3

Posted in Flash, Flex by Ben Clinkinbeard on the July 21st, 2006

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:

PHP:
  1. $formattedDate = date($myDateString, 'd/m/Y');

Well, you can imagine my horror (exaggerate much?) when I discovered that the same functionality in AS3 looked like this:

Actionscript:
  1. var df:DateFormatter = new DateFormatter();
  2. df.formatString = "MM/DD/YYYY";
  3. var formattedDate:String = df.format(myUnformattedString);

My solution? QuickDateFormatter, whose usage looks like this:

Actionscript:
  1. var formattedDate:String = QuickDateFormatter.format(myUnformattedString, "MM/DD/YYYY");

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.

Actionscript:
  1. package com.fmr.utils
  2. {
  3.     import mx.formatters.DateFormatter;
  4.  
  5.     public class QuickDateFormatter
  6.     {
  7.         public static function format(str_dateString:String, str_dateFormat:String):String
  8.         {
  9.             var f:DateFormatter = new DateFormatter();
  10.             f.formatString = str_dateFormat;
  11.             return f.format(str_dateString);
  12.         }
  13.     }
  14. }

Enjoy!

9 Responses to 'QuickDateFormatter - efficient date formatting in AS3'

Subscribe to comments with RSS or TrackBack to 'QuickDateFormatter - efficient date formatting in AS3'.

  1. Tom Chiverton said,

    on July 26th, 2006 at 10:34 am

    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 ?

  2. Ben said,

    on July 26th, 2006 at 10:46 am

    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.

  3. Jeff said,

    on December 23rd, 2006 at 3:46 am

    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

  4. Ben said,

    on December 23rd, 2006 at 12:24 pm

    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.

  5. Mike Bernstein said,

    on March 7th, 2007 at 8:56 am

    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.

  6. Mike Bernstein said,

    on March 7th, 2007 at 10:40 am

    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.

  7. Dinesh erandika said,

    on April 9th, 2007 at 5:10 am

    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

  8. Dinesh erandika said,

    on April 9th, 2007 at 5:13 am

    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

  9. Dinesh erandika said,

    on April 9th, 2007 at 5:16 am

    sorry jeff mxml is here

Leave a Reply