How to create an ActionScript 3.0 in Flash to display Current Date, Time and Day of Week

1.8k views Asked by At

I am trying to create a new ActionScript 3.0 file in Flash that will include the Current Time, Date and Day of the week. I am able to complete the script separately (Time and Date) and (DayofWeek) but when combining I receive line errors 1120:Access of undefined property time. I am not a GURU but am a pretty fast learning. I would appreciate any help that can be provided to me. Also I would like for the time to display AM\PM and to show in double digits for numbers ranging from 1-9 with a "0" added. Thank you in advance

Here is my code below

var looper:Timer = new Timer(100);
looper.start();
looper.addEventListener(TimerEvent.TIMER, looptime)

function looptime(event:TimerEvent):void{
    var dayOfWeek_array:Array = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); 
    var today_date:Date = new Date();
    var month:Number = time.getMonth()+1;
    var day:Number = time.getDate();
    var year:Number = time.getFullYear();


    var day_str:String = dayOfWeek_array[today_date.getDay()];
    var hour:Number = time.getHours();
    var minute:Number = time.getMinutes(); 

    Day_txt.text = day_str;
    Date_txt.text = month + "-" + day + "-" + year;
    if (hour > 12){
        Time_txt.text = hour-12 + ":" + minute;
    }else if (minute < 10){
         Time_txt.text = hour + ":" + "0" + minute;
    } else {
         Time_txt.text = hour + ":" + minute;
         }
    //Time_txt.text = hour + ":" + minute;

    }
1

There are 1 answers

6
HITMAN On BEST ANSWER

your coding has some little problems.for example you don't need to define dayOfWeek_array every second.this makes your app slow.so put it out of your looptime function.and better to use uint type instead of Number for variables that won't contain decimal numbers.

This is the full code(AM/PM system).you can copy this:

import flash.utils.Timer;

var looper:Timer=new Timer(1000);

var dayOfWeek_array:Array = new Array("Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"); 


var time:Date;
//use uint type instead of number for these vars.because they won't contain decimals!

var month:uint;
var day:uint;
var year:uint; 


var hour:String;
var minute:String;     
var day_str:String;

var hour12:uint;
var AM_PM:Object;




looper.start();
looper.addEventListener(TimerEvent.TIMER, looptime)

//execute the function when starting app
looptime(null);

function looptime(event:TimerEvent):void{
    time=new Date();

    month=time.getMonth();
    day=  time.getDate()+1;
    year= time.getFullYear();

    hour12=time.getHours()>11?time.getHours()-12:time.getHours();
    AM_PM=String(time.getHours()>11?" PM":" AM");
    time.getMonth();

    hour= hour12>9?String(hour12):"0"+String(hour12);
    minute=time.getMinutes()>9?String(time.getMinutes()):"0"+String(time.getMinutes());
    day_str=dayOfWeek_array[time.getDay()]


    Day_txt.text = day_str;
    Date_txt.text = month + "-" + day + "-" + year;

    Time_txt.text = hour + ":" + minute+AM_PM;
}

Hope this helps.☺