ActionScript 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(2011, 0, 31);//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.采用拼接字符串的形式,不再细述。


转载于:https://www.cnblogs.com/Bill_Lee/archive/2011/03/30/1999696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值