Returns the number of milliseconds since midnight January 1, 1970, universal time, for a Date object. Date[r]
Trang 1LỚP DATE VÀ TIME
Ngày và giờ là một vấn ñề rất quan trọng trong các chương trình ứng dụng
Trong Flash AS 3.0 thời gian ñược tính theo ñơn vị miligiây và theo toạ ñộ thời gian UTC (Coordinated Universal Time ) với mốc thời gian là 0h 0 ph 0 giây ngày 01/01/1970
Lấy ngày giờ hiện tại của máy:
// Tạo một ñối tượng ngay
var current:Date = new Date( );
trace(“ngày hiện tại:”+current);
kết quả : ngày hiện tại Sat Sep 18 15:20:46 GMT+0700 2010
ví dụ :
var d:Date=new Date();
trace(d);
var count:Number=1;
var tTimer:Timer = new Timer(1000, 3);
tTimer.addEventListener(TimerEvent.TIMER, onTimer);
tTimer.start( );
function onTimer(event:TimerEvent):void {
var d1:Date=new Date();
trace("Goi ham lan "+count.toString()+" sau :"+(d1.time -d.time).toString()+" Miligiay." );
count++;
}
Kết quả:
Sat Sep 18 15:43:59 GMT+0700 2010
Goi ham lan 1 sau :1000 Miligiay
Goi ham lan 2 sau :2062 Miligiay
Goi ham lan 3 sau :3109 Miligiay
Một số thuộc tính :
Trang 2fullYear : lấy năm ñầy ñủ , ví dụ 2010
date: ngày của tháng ( từ 1 ñến 31)
month: lấy tháng ( từ 0 ñến 11)
day: lấy ngày của tuần ( từ 0 ñến 6)
hours: lấy giờ (từ 0 ñến 23)
minutes : lấy phút (Từ 0 ñến 59)
seconds : lấy giây (Từ 0 ñến 59)
miliseconds: lấy giây (Từ 0 ñến 999)
Ví dụ :
var d:Date=new Date();
trace(d);
trace("d.month:="+d.month );
trace("d.day:="+d.day);
trace("d.date:="+d.date);
trace("d.fullYear:="+d.fullYear);
trace("d.hours:="+d.hours);
trace("d.minutes :="+d.minutes );
trace("d.seconds:="+d.seconds );
trace("d.milliseconds:="+d.milliseconds); Kết quả:
Sat Sep 18 16:19:35 GMT+0700 2010
d.month:=8
d.day:=6
d.date:=18
d.fullYear:=2010
d.hours:=16
d.minutes :=19
Trang 3d.seconds:=35
d.milliseconds:=812
Ví dụ sau tính thời gian từ khi chương trình bắt ñầu chạy tới thời ñiểm hện tại tính theo mili giây: Lưu ý là trong ví dụ có dùng lớp Timer Timer có thể có 1 hay 2 tham số:
Timer(số mili giây chờ);
Timer( số mili giây chờ, số lần bộ ñếm thời gian thực hiện)
import flash.display.Sprite;
import flash.text.*;
import flash.utils.Timer ;
import flash.events.TimerEvent;
import flash.utils.getTimer;
var _text:TextField;
var _start:uint;
_start = getTimer( );
_text = new TextField( );
addChild(_text);
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start( );
function onTimer(event:TimerEvent):void {
_text.text = (getTimer( ) - _start) + " milliseconds"; }
Lưu ý : ðể hiển thị theo giờ , phút ,giây ta phải qui ñổi thời gian, Ví dụ sau minh hoạ thời gian ñếm ngược có qui ñổi thời gian Ở ñây sau khi hết thời gian ta cho timer dừng lại bằng cách dùng
_timer.stop() , nếu không timer vẫn ñếm và sẽ cho kết quả thời gian âm
Trang 4
import flash.display.Sprite;
import flash.text.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.utils.getTimer;
var _text:TextField;
var _timer:Timer;
var _start:uint;
var _count:uint = 20;
_start = getTimer( );
_text = new TextField( );
var formatter1:TextFormat = new TextFormat( ); formatter1.font="Arial";
formatter1.size =20;
_text.defaultTextFormat= formatter1;
_text.y=stage.stageHeight/2;
_text.x=stage.stageWidth/2;
_text.width =150;
addChild(_text);
_timer = new Timer(500);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
Trang 5_timer.start( );
function onTimer(event:TimerEvent):void {
var elapsed:int = Math.round(_count - (getTimer( ) - _start) / 1000);
_text.text = elapsed + " seconds";
if(elapsed < 0) {
_text.text = " -";
_timer.stop( );
}
}
Kết quả :
Lớp thời gian còn nhiều thuộc tính tương tự như trong AS 2.0
Trang 6Thuộc tính
The day of the month (an integer from 1 to 31) specified by a Date object according to local
time
Date
The day of the month (an integer from 1 to 31) of a Date object according to universal time
(UTC)
Date
[read-only] The day of the week (0 for Sunday, 1 for Monday, and so on) specified by this
Date according to local time
Date
[read-only] The day of the week (0 for Sunday, 1 for Monday, and so on) of this Date
according to universal time (UTC)
Date
The full year (a four-digit number, such as 2000) of a Date object according to local time Date
The four-digit year of a Date object according to universal time (UTC) Date
The hour (an integer from 0 to 23) of the day portion of a Date object according to local time Date
The hour (an integer from 0 to 23) of the day of a Date object according to universal time
(UTC)
Date
The milliseconds (an integer from 0 to 999) portion of a Date object according to local time Date
The milliseconds (an integer from 0 to 999) portion of a Date object according to universal
time (UTC)
Date
The minutes (an integer from 0 to 59) portion of a Date object according to local time Date
The minutes (an integer from 0 to 59) portion of a Date object according to universal time
(UTC)
Date
The month (0 for January, 1 for February, and so on) portion of a Date object according to
local time
Date
The month (0 [January] to 11 [December]) portion of a Date object according to universal
time (UTC)
Date
prototype : Object
[static] A reference to the prototype object of a class or function object Object
The seconds (an integer from 0 to 59) portion of a Date object according to local time Date
The seconds (an integer from 0 to 59) portion of a Date object according to universal time Date
Trang 7(UTC)
The number of milliseconds since midnight January 1, 1970, universal time, for a Date
object
Date
[read-only] The difference, in minutes, between universal time (UTC) and the computer's
local time
Date Public Methods
Phương thức
Date(yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0,
minute:Number = 0, second:Number = 0, millisecond:Number = 0)
Constructs a new Date object that holds the specified date and time
Date
Returns the day of the month (an integer from 1 to 31) specified by a Date object according
to local time
Date
Returns the day of the week (0 for Sunday, 1 for Monday, and so on) specified by this Date according to local time
Date
Returns the full year (a four-digit number, such as 2000) of a Date object according to local time
Date
Returns the hour (an integer from 0 to 23) of the day portion of a Date object according to
local time
Date
Returns the milliseconds (an integer from 0 to 999) portion of a Date object according to
local time
Date
Returns the minutes (an integer from 0 to 59) portion of a Date object according to local
time
Date
Returns the month (0 for January, 1 for February, and so on) portion of this Date according
to local time
Date
Returns the seconds (an integer from 0 to 59) portion of a Date object according to local
time
Date
Returns the number of milliseconds since midnight January 1, 1970, universal time, for a
Date object
Date
Returns the difference, in minutes, between universal time (UTC) and the computer's local time
Date
Trang 8Returns the day of the month (an integer from 1 to 31) of a Date object, according to
universal time (UTC)
Returns the day of the week (0 for Sunday, 1 for Monday, and so on) of this Date according
to universal time (UTC)
Date
Returns the four-digit year of a Date object according to universal time (UTC) Date
Returns the hour (an integer from 0 to 23) of the day of a Date object according to universal time (UTC)
Date
Returns the milliseconds (an integer from 0 to 999) portion of a Date object according to
universal time (UTC)
Date
Returns the minutes (an integer from 0 to 59) portion of a Date object according to
universal time (UTC)
Date
Returns the month (0 [January] to 11 [December]) portion of a Date object according to
universal time (UTC)
Date
Returns the seconds (an integer from 0 to 59) portion of a Date object according to
universal time (UTC)
Date
Indicates whether an object has a specified property defined Object
Indicates whether an instance of the Object class is in the prototype chain of the object
specified as the parameter
Object
[static] Converts a string representing a date into a number equaling the number of
milliseconds elapsed since January 1, 1970, UTC
Date
Indicates whether the specified property exists and is enumerable Object
Sets the day of the month, according to local time, and returns the new time in milliseconds Date
Sets the year, according to local time, and returns the new time in milliseconds Date
Sets the hour, according to local time, and returns the new time in milliseconds Date
Sets the milliseconds, according to local time, and returns the new time in milliseconds Date
Sets the minutes, according to local time, and returns the new time in milliseconds Date
Sets the month and optionally the day of the month, according to local time, and returns the Date
Trang 9new time in milliseconds
Sets the availability of a dynamic property for loop operations Object
Sets the seconds, according to local time, and returns the new time in milliseconds Date
Sets the date in milliseconds since midnight on January 1, 1970, and returns the new time in milliseconds
Date
Sets the day of the month, in universal time (UTC), and returns the new time in
milliseconds
Date
Sets the year, in universal time (UTC), and returns the new time in milliseconds Date
millisecond:Number):Number
Sets the hour, in universal time (UTC), and returns the new time in milliseconds
Date
Sets the milliseconds, in universal time (UTC), and returns the new time in milliseconds Date
Sets the minutes, in universal time (UTC), and returns the new time in milliseconds Date
Sets the month, and optionally the day, in universal time(UTC) and returns the new time in
milliseconds
Date
Sets the seconds, and optionally the milliseconds, in universal time (UTC) and returns the
new time in milliseconds
Date
Returns a string representation of the day and date only, and does not include the time or
timezone
Date
Returns a String representation of the day and date only, and does not include the time or
timezone
Date
toLocaleString():String
Returns a String representation of the day, date, time, given in local time Date
Returns a String representation of the time only, and does not include the day, date, year, or
timezone
Date
toString():String
Returns a String representation of the day, date, time, and timezone Date
Returns a String representation of the time and timezone only, and does not include the day
and date
Date
Returns a String representation of the day, date, and time in universal time (UTC) Date
Trang 10
UTC(year:Number, month:Number, date:Number = 1, hour:Number = 0, minute:Number =
0, second:Number = 0, millisecond:Number = 0):Number
[static] Returns the number of milliseconds between midnight on January 1, 1970, universal
time, and the time specified in the parameters
Date
Returns the number of milliseconds since midnight January 1, 1970, universal time, for a
Date object
Date
Ví dụ sau xuất ra ngày tháng tiếng Việt không dáu Muốn có dấu trong trang viết code ta thay ñổi kiểu gõ TCVN và font dạng VNI hổ trợ Unicode
var Thang:Array = new Array("Thang gieng","Thang Hai","Thang Ba", "Thang Tu","Thang Nam","Thang Sau",
"Thang Bay","Thang Tam","Thang Chin",
"Thang Muoi","Thang Muoi mot", "Thang Muoi Hai"); var Thungay:Array = new Array("Chu Nhat","Thu Hai","Thu Ba",
"Thu Tu","Thu Nam","Thu Sau","Thu Bay")
var now:Date = new Date();
trace(Thungay[now.getUTCDay()]+" ngay "+now.getUTCDate()
+" "+ Thang[now.getUTCMonth()]+" nam "+
now.getUTCFullYear());
Kết quả:
Thu Bay ngay 18 Thang Chin nam 2010
-
Ví dụ thêm và không thêm số 0 trong cách viết giờ phút:
var someday:Date = new Date(2010, 18, 09, 15, 5);
trace(someday);
Trang 11trace(someday.getHours() + ":" + someday.getMinutes());
var gio_cua_may_ban:String = gio_kieu_USA(someday.getHours(), someday.getMinutes());
trace(gio_cua_may_ban);
function gio_kieu_USA(hrs:uint, mins:uint):String {
var sang_chieu:String = "PM";
var Phut:String = kieu_hai_so(mins);
if(hrs > 12) {
hrs = hrs-12;
} else if(hrs == 0) {
sang_chieu = "AM";
hrs = 12;
} else if(hrs < 12) {
sang_chieu = "AM";
}
return (kieu_hai_so(hrs) + ":" + Phut + " " + sang_chieu); }
function kieu_hai_so(num:uint):String {
if(num < 10) {
return ("0" + num);
}
return num.toString();
}
Trang 12Kết quả:
Sat Jul 9 15:05:00 GMT+0700 2011 15:5
03:05 PM