« Diamonds.. mass produced in factories. SharpFlash.. on SourceForge!! »

August 27, 2003

dateFormat ActionScript port

Coming from a ColdFusion background (before I started using Flash) I used to take advantage of the DateFormat function. I've always wondered why there was never a Flash version. Well.. now there is! I'm actually surprised I haven't seen this function created before.

Click here to download my ActionScript version of ColdFusion's DateFormat function. Some example usages:

#include "dateFormat.as"

testDate = new Date(2003, 7, 9);
trace(dateFormat(testDate, "mmm-dd-yyyy"));
trace(dateFormat(testDate, "mmmm d, yyyy"));
trace(dateFormat(testDate, "mm/dd/yyyy"));
trace(dateFormat(testDate, "d-mmm-yyyy"));   
trace(dateFormat(testDate, "ddd, mmmm dd, yyyy"));
trace(dateFormat(testDate, "short"));
trace(dateFormat(testDate, "medium"));
trace(dateFormat(testDate, "long"));
trace(dateFormat(testDate, "full"));

Comments

  • Nice code but unfortunately it only works for people whose short date formats are mm-dd-yyyy. Go north to Canada or across the pond and people commonly have Windows set to the short date format of dd/mm/yyyy. I wish their was a way to get this short date format from within Flash, but apparently there is not.

  • Eric - yes it does!

    #include "dateFormat.as"

    testDate = new Date();
    trace(dateFormat(testDate, "dd/mm/yyyy"));

    The above traces 17/12/2003, since today is December 12th. By changing the mask you can change how the date is displayed.

    If you don't like how the "short", "full", etc. masks are implemented, then it's a bigger issue you need to take up with Macromedia. I just ported what is Standard in ColdFusion.

  • beautiful function! Thanks!

    V

  • can't run in it in AS2 by the way

  • well correction there, I can run it in AS2 but there's an error when I initially load the as file
    Something about AS2 not being ablt to do something with classes. Another thing, What about a TimeFormat() Function :)

    V

  • Note - if you're getting a 403 forbidden trying to download the file, try this instead:

    http://www.darronschall.com/downloads/dateFormat.zip

  • Can this be used to convert an dd/mm/aaaa String to a dateformat?

  • John O. wrapped my AS1 code in a reusable class for AS2:

    http://www.yapiodesign.com/blog/2005/11/10/dateformatter-static-class-update-of-darron-schalls-dateformatas/

  • Darron,
    This is great. Thanks. Know of anyone else working to extend the date class? I would like to see something more along the lines of what PHP offers ... especially functions that return the day of the year (0 - 365/366) or the week number of a current day (0-52) ... something you can use more for calculations as opposed to display.

    J