actionscript3 Date(日期)

Package Top Level

Class     public final dynamic class Date

Inheritance Date->ObjectLanguage

Version: ActionScript 3.0

Runtime Versions: Flash Player 9, AIR 1.0, Flash Lite 4



The Date class represents date and time information. An instance of the Date class represents a particular point in time for which the properties such as month, day, hours, and seconds can be queried or modified. The Date class lets you retrieve date and time values relative to universal time (Greenwich mean time, now called universal time or UTC) or relative to local time, which is determined by the local time zone setting on the operating system that is running Flash Player. The methods of the Date class are not static but apply only to the individual Date object specified when the method is called. The Date.UTC() and Date.parse() methods are exceptions; they are static methods.

 

To use the Date class, construct a Date instance using the new operator.

ActionScript 3.0 adds several new accessor properties that can be used in place of many Date class methods that access or modify Date instances. ActionScript 3.0 also includes several new variations of the toString() method that are included for ECMA-262 3rd Edition compliance, including: Date.toLocaleString()Date.toTimeString()Date.toLocaleTimeString()Date.toDateString(), and Date.toLocaleDateString().

To compute relative time or time elapsed, see the getTimer() method in the flash.utils package.

 

Property And Method Detail                                         

設置和取得各部分的值(年,月,日,小時,分鐘,秒,毫秒)

How to get data from Date object:

Year:(such as 2011)

1. fullYear

 

function get fullYear():Number
funtion set fullYear(value:Number):void

The full year (a four-digit number, such as 2000) of a Date object according to local time

2. getFullYear():Number

Returns the full year (a four-digit number, such as 2000) of a Date object according to local time.

3. function setFullYear(year:Number, month:Number, day:Number):Number

Sets the year, according to local time, and returns the new time in milliseconds

注意,該函數可以設置月和天,月是從0開始的

Example:

 

private function testFullYear():void
 
{
 
  var d:Date new Date();
 
  d.fullYear = 2011;
 
  trace(d.fullYear);
 
  d.setFullYear(2012,0,2);
 
  trace(d.date);
 
  trace(d.getFullYear());
 
}

OutPut:

2011

2

2012

 

 

Month:

The month (0 for January, 1 for February, and so on) portion of a Date object according to local time. Local time is determined by the operating system on which the Flash runtimes are running.

The month (0 - 11) portion of a Date object.

1. month:

 

function get month():Number
function set month(value:Number):void

2. function getMonth():Number

3. function setMonth(month:Number, day:Number):void

Example:

 

private function testMonth():void
{
 
  var monthLabels:Array new Array("January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December");
 
 
  var dt:Date new Date(2011,0,1);
  trace(dt.getMonth());
  trace(monthLabels[dt.getMonth()]);
 
  dt.setMonth(1);
  trace(dt.getMonth());             // 1
  trace(monthLabels[dt.getMonth()]); // February
 
  dt.date = 20;
  dt.month = 2;
 
  trace(dt.month);// 3
  trace(monthLabels[dt.month]);// March
}

OutPut

0

January

1

February

2

March

 

 

Date:

date(月中的天)

The day of the month (an integer from 1 to 31) specified by a Date object according to local time. Local time is determined by the operating system on which the Flash runtimes are running.

1. date

 

function get date():Number
 
function set date(value:Number):void
 
function getDate():Number

Returns the day of the month (an integer from 1 to 31) specified by a Date object according to local time. Local time is determined by the operating system on which the Flash runtimes are running

function setDate(day:Number):Number

Sets the day of the month, according to local time, and returns the new time in milliseconds. Local time is determined by the operating system on which the Flash runtimes are running.

Example:

 

private function testDate():void
{
  var dt:Date new Date(2011,0,1);
  dt.date = 2;
  trace(dt.date);
  dt.setDate(20);
  trace(dt.getDate());
}

OutPut

2

20

 

Hour:

1.hours

function get hours():Number

function set hours(value:Number):void

The hour (an integer from 0 to 23) of the day portion of a Date object according to local time. Local time is determined by the operating system on which the Flash runtimes are running.

2. function getHours():Number

3. function setHours(hour:Number, minute:Number, second:Number, millisecond:Number):Number

Sets the hour, according to local time, and returns the new time in milliseconds. Local time is determined by the operating system on which the Flash runtimes are running.

 

minutes, seconds, milliseconds都如hours

 

小結:

以上可以看出基本上有規律:

可以取得年,月,日,小時,分鐘,秒,微秒等部分的值。可以直接通過屬性(fullYear, month, date, hours, minutes, seconds, milliseconds)來設置各個部分。也可以通過類似getFullYear的含有get的函數獲得各個部分的值,這和屬性沒有區別。但是通過setFullYear(),setMonth(),setDate()等set函數可以設置從高到低的值,例如setFullYear(),可以直接設置年,月,日;基本上分爲兩階,一個是年,月,日,一個是小時,分鐘,秒,毫秒。

 

注意:

以上講述了設置方法,但是設置的時候不會有異常拋出,也不會有設置成功與否的標誌。所有的設置都會成功,哪怕是錯誤的日期,但是未必能得到設置的值。這點必須注意,如下例:

 

private function testSettingDate():void
{
  var dt:Date new Date(2011031);//2011.1.31
  trace(dt);
  dt.month = 1;// set the month to February
  trace(dt.month);
  trace(dt);
}

輸出

Mon Jan 31 00:00:00 GMT+0800 2011

2

Thu Mar 3 00:00:00 GMT+0800 2011

可以看出,當設置2月的時候,再獲取,沒有得到1,而是2,因爲dt的初始值是2011.1.31,直接設置月份就會成爲2011.2.31,這是不存在的時間,但是Date類會更具年,月,日不同部分計算成毫秒,當再次取其月份的時候,通過計算獲得。因此,單獨設置年,月,日,小時,分鐘,秒,毫秒的時候需要特別注意。

 

關於時間的計算:

計算之前必須瞭解time屬性,這是Date對象的核心內容。time屬性記錄了從1970.1.1開始的毫秒數。由此屬性可以進行任意的時間計算。Date類並不提供日期之間的計算,可以通過各部分的加減或者毫秒級的加減實現。

1. time屬性

1.1.    time (property)

The number of milliseconds since midnight January 1, 1970, universal time, for a Date object. Use this method to represent a specific instant in time when comparing two or more Date objects.

function get time():Number

function set time(value:Number):void

1.2.    function getTime():Number

1.3.    function setTime(millisecond:Number):Number

Sets the date in milliseconds since midnight on January 1, 1970, and returns the new time in milliseconds.

 

2. 毫秒,秒,分鐘,小時,天的加減

2.1.    天的加法

 

public function addDays(date:Date, days:Number):Date
{
  date.time += 1000*60*60*24*days;
  return date;
}
 
public function addDays1(date:Date, days:Number):Date
{
  date.date += days;
  return date;
}

這兩種方法能實現相同的功能,即在當前的日期基礎上加上規定的天數。

測試:

 

var dt1:Date new Date(2011,0,1,12,0,0,0);
trace(this.addDays(dt1, 45).time);
var dt2:Date new Date(2011,0,1,12,0,0,0);
trace(this.addDays(dt2, 45).time);

輸出:

1297742400000

1297742400000

可見,雖然date屬性的返回值只在1-31之間,但是可以加上任意的天數。

 

小時,分鐘的加法

 

public function addMinutes(date:Date, minutes:Number):Date
{
  date.time += 1000*60*minutes;
  return date;
}
 
public function addMinutes1(date:Date, minutes:Number):Date
{
  date.minutes += minutes;
  return date;
}
 
public function addHours(date:Date, hours:Number):Date
{
  date.time += 1000*60*60*hours;
  return date;
}
 
public function addHours1(date:Date, hours:Number):Date
{
  date.time += 1000*60*60*hours;
  return date;
}

秒和毫秒類似。

2.2 年,月的加減法

月的加法要注意例如2011.1.31,如果對此月加上1,那麼就是2011.2.31,這是不存在的日期,如下代碼:

 

var dt1:Date new Date(2011,0,31);
dt1.month += 1;
trace(dt1);

輸出:

Thu Mar 3 00:00:00 GMT+0800 2011

可以看出,結果是2011.3.3,因此要有效的加減月份,應當設置爲月初(默認)。

3. 某段時間內的總天數(包括起始時間)

 

public function calculateDays(start:Date, end:Date):Number
{
    return (end.time-start.time)/1000/60/60/24+1;
}

 

當然,這裏,start和end的小時,分鐘,秒和毫秒都必須是0,否則就會是小數,是精確到天以下的。

 

 

關於時間的格式化輸出

 

  1. 使用DateFormatter類來實現。

先設置formatString,再調用format函數。

 

<?xml version="1.0" encoding="utf-8"?>
 
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"
 
   creationComplete="init()">
 
    <mx:Script>
 
       <![CDATA[
 
           import mx.formatters.DateFormatter;
 
           public function init():void
 
           {
 
              var d:Date new Date();
 
              trace(flexDateToCRMDate(d));
 
           }
 
 
 
           public function flexDateToCRMDate(date:Date):String
 
           {
 
              var f:DateFormatter = new DateFormatter();
 
              f.formatString = "YYYY-MM-DD";
 
              return f.format(date);
 
           }
 
       ]]>
 
    </mx:Script>
 
</mx:WindowedApplication>

2.採用拼接字符串的形式,不再細述。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章