开发者

AS3 - Date January-01-2011

开发者 https://www.devze.com 2023-03-17 00:11 出处:网络
Ho开发者_开发百科w can I get a date in a format like this out of Flash AS3? January-01-2011 ThanksYou can use the formatDate() function I made in the following example to format the date the way

Ho开发者_开发百科w can I get a date in a format like this out of Flash AS3?

January-01-2011

Thanks


You can use the formatDate() function I made in the following example to format the date the way you want it:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var date:Date = new Date();

            trace(formatDate(date)); // output: June-02-2011

        }// end function

        private function formatDate(date:Date):String
        {
            var month:String, dateString:String, year:String;

            var months:Array = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

            month = months[date.getMonth() - 1]

            dateString = (date.getDate() < 10) ? "0" + date.getDate().toString() : date.getDate().toString();

            year = date.getFullYear().toString();

            return month + "-" + dateString + "-" + year; 

        }// end function

    }// end class

}// end package


create a formatted string using the Date class with an array of strings for each month. the Date class is a very broad top level class with lots of functionality. the attached link to the documentation includes sample code. it's easily tailorable to your needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消