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.
Posted by: Eric D at 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 at December 17, 2003 12:52 PM
beautiful function! Thanks!
V
Posted by: Valentin at July 13, 2004 01:07 PM
can't run in it in AS2 by the way
Posted by: Valentin at July 13, 2004 01: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 at 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 at September 19, 2005 06:39 AM
Can this be used to convert an dd/mm/aaaa String to a dateformat?
Posted by: Martin at November 11, 2005 02: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 at November 15, 2005 07: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 at February 23, 2006 10:24 AM