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"));

9 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.
Posted by: Eric D | December 17, 2003 12:23 PM
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.
Posted by: darron | December 17, 2003 12:52 PM
beautiful function! Thanks!
V
Posted by: Valentin | July 13, 2004 1:07 PM
can't run in it in AS2 by the way
Posted by: Valentin | July 13, 2004 1:53 PM
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
Posted by: Valentin | July 16, 2004 12:44 PM
Note - if you're getting a 403 forbidden trying to download the file, try this instead:
http://www.darronschall.com/downloads/dateFormat.zip
Posted by: darron | September 19, 2005 6:39 AM
Can this be used to convert an dd/mm/aaaa String to a dateformat?
Posted by: Martin | November 11, 2005 2:12 AM
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/
Posted by: darron | November 15, 2005 7:02 PM
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
Posted by: J Marziani | February 23, 2006 10:24 AM