Sietse
Sietse

Reputation: 7955

How do I format a Date in Flash?

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?

Upvotes: 9

Views: 25119

Answers (5)

T Graham
T Graham

Reputation: 1339

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

Upvotes: 30

Ryan Williams
Ryan Williams

Reputation: 1620

private static function getTime():String
{
    const d = '.';
    const e = '';
    const s = ':';

    var date:Date = new Date();
    return e.concat(pad(date.hours, 2), s, 
                    pad(date.minutes, 2), s, 
                    pad(date.seconds, 2), d, 
                    pad(date.milliseconds, 3));

    function pad(value:String, length:int):String
    {
        const zero = '0';
        var result:String = value;
        while (result.length < length)
        {
            result = zero.concat(result);
        }
        return result;
    }
}

Upvotes: 0

Eric Smith
Eric Smith

Reputation: 2899

There is the mx.formatters.DateFormatter class (see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/formatters/DateFormatter.html). It has a formatString property for custom formatting. You can then call format, supplying a Date to get a string.

Upvotes: 1

DannyC
DannyC

Reputation: 79

Unfortunately I don't think you are overlooking anything in terms of native support. There is this project which seems to offer a bit more flexibility, however I have not ever got round to working with it in any depth so I can't vouch for it. The project I am currently working on has a 500 line (and counting) DateUtil class as a result..

Upvotes: 1

Guillaume Malartre
Guillaume Malartre

Reputation: 425

Here's a simple example of a custom format:

    public static function getDateIso8601Long(date:Date):String {
        var str:String = date.getFullYear().toString()
        str = str +"-"+ ((String((date.getMonth()+1)).length == 1)?"0"+(date.getMonth()+1):(date.getMonth()+1)).toString()
        str = str +"-"+ ((date.getDate().toString().length == 1)?"0"+date.getDate():date.getDate()).toString()
        str = str +"T"+ ((date.getHours().toString().length == 1)?"0"+date.getHours():date.getHours()).toString()
        str = str +":"+ ((date.getMinutes().toString().length == 1)?"0"+date.getMinutes():date.getMinutes()).toString()
        str = str +":"+ ((date.getSeconds().toString().length == 1)?"0"+date.getSeconds():date.getSeconds()).toString()
        var ms:String = date.getMilliseconds().toString()
        while (ms.length < 3)
            ms = "0"+ms
        str = str+"."+ms
        var offsetMinute:Number = date.getTimezoneOffset()
        var direction:Number = (offsetMinute<0)?1:-1
        var offsetHour:Number = Math.floor(offsetMinute/60)
        offsetMinute = offsetMinute-(offsetHour*60)

        var offsetHourStr:String = offsetHour.toString()
        while (offsetHourStr.length < 2)
            offsetHourStr = "0"+offsetHourStr
        var offsetMinuteStr:String = offsetMinute.toString()
        while (offsetMinuteStr.length < 2)
            offsetMinuteStr = "0"+offsetMinuteStr
        str = str+((direction == -1)?"-":"+")+offsetHourStr+":"+offsetMinuteStr
        return str 
    } 

Upvotes: 2

Related Questions