如何在 Flash 中创建 ActionScript 3.0 以显示当前日期、时间和星期几
How to create an ActionScript 3.0 in Flash to display Current Date, Time and Day of Week
我正在尝试在 Flash 中创建一个新的 ActionScript 3.0 文件,其中将包括当前时间、日期和星期几。我能够分别完成脚本(时间和日期)和(星期几),但是在组合时我收到未定义 属性 时间的行错误 1120:Access。我不是古鲁,但学得很快。如果能提供给我任何帮助,我将不胜感激。此外,我希望有时间显示 AM\PM 并以两位数显示 1-9 范围内的数字并添加“0”。提前谢谢你
下面是我的代码
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;
}
你的编码有一些小的 problems.for 例子你不需要定义 dayOfWeek_array
每个 second.this 让你的应用程序 slow.so 把它从你的 looptime
function.and 对于不包含小数的变量,最好使用 uint
类型而不是 Number
。
这是完整的代码(AM/PM系统)。你可以复制这个:
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;
}
希望对您有所帮助。☺
我正在尝试在 Flash 中创建一个新的 ActionScript 3.0 文件,其中将包括当前时间、日期和星期几。我能够分别完成脚本(时间和日期)和(星期几),但是在组合时我收到未定义 属性 时间的行错误 1120:Access。我不是古鲁,但学得很快。如果能提供给我任何帮助,我将不胜感激。此外,我希望有时间显示 AM\PM 并以两位数显示 1-9 范围内的数字并添加“0”。提前谢谢你
下面是我的代码
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;
}
你的编码有一些小的 problems.for 例子你不需要定义 dayOfWeek_array
每个 second.this 让你的应用程序 slow.so 把它从你的 looptime
function.and 对于不包含小数的变量,最好使用 uint
类型而不是 Number
。
这是完整的代码(AM/PM系统)。你可以复制这个:
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;
}
希望对您有所帮助。☺