How do I format a Date in Flash?

Sietse picture Sietse · Sep 15, 2010 · Viewed 24.4k times · Source

I could be overlooking something, but Flash / AS3 seems to be missing basic date formatting functionality. How do I get a formatted string from a Date ? There's a few options like .toLocaleDateString() and .toUTCString(), but that a bit limiting, to say the least.

So, how do I format a Date object in AS3?

Answer

T Graham picture T Graham · Jun 7, 2012

Flash Player 10.1 added a DateTimeFormatter as part of the flash.globalization package. It's flexible but not well documented.

import flash.globalization.DateTimeFormatter;

var d:Date = new Date();
var dtf:DateTimeFormatter = new DateTimeFormatter("en-US");
dtf.setDateTimePattern("yyyy-MM-dd 'at' hh:mm:ssa");
trace(dtf.format(d)); 

// 2012-06-06 at 09:58:46PM