AS3:时间戳的转换 http://blog.csdn.net/a7719665/article/details/5912004#

来源:互联网 发布:口口网络用语什么意思 编辑:程序博客网 时间:2024/06/03 20:26

Date 类是Flash ActionScript3.0中构造函数最多变的类之一,我们可以 通过以下几种方法定义Date类。

第一,如果未给定参数,则 Date() 构造函数将按照您所在时区的本地时间返回包含当前日期和时间的 Date 对象。

var now:Date = new Date();

第二,如果仅给定了一个数字参数,则 Date() 构造函数将其视为自 1970 年 1 月 1 日以来经过的毫秒数,并且返回对应的 Date 对象。请注意,您传入的毫秒值将被视为自 1970 年 1 月 1 日(UTC 时间)以来经过的毫秒数。但是,该 Date 对象会按照您所在的本地时区来显示值,除非您使用特定于 UTC 的方法来检索和显示这些值。如果仅使用一个毫秒参数来创建新的 Date 对象,则应确保考虑到您的当地时间和 UTC 之间的时区差异。以下语句创建一个设置为 1970 年 1 月 1 日午夜(UTC 时间)的 Date 对象:
var millisecondsPerDay:int = 1000 * 60 * 60 * 24;// 获取一个表示自起始日期 1970 年 1 月 1 日后又过了一天时间的 Date 对象var startTime:Date = new Date(millisecondsPerDay);

第三,您可以将多个数值参数传递给 Date() 构造函数。该构造函数将这些参数分别视为年、月、日、小时、分钟、秒和毫秒,并将返回一个对应的 Date 对象。假定这些输入参数采用的是本地时间而不是 UTC。以下语句获取一个设置为 2000 年 1 月 1 日开始的午夜(本地时间)的 Date 对象:
var millenium:Date = new Date(2000, 0, 1, 0, 0, 0, 0);


  1. package com.xygame.module.util  
  2. {  
  3.     public class TimeTransform  
  4.     {  
  5.         public   static var timeTrans:TimeTransform;  
  6.         public var date:Date;  
  7.         public function TimeTransform()  
  8.         {  
  9.             if(timeTrans!=null){  
  10.                 throw new Error("单例类只能被实例化一次");  
  11.             }  
  12.         }  
  13.         public static function getInstance():TimeTransform{  
  14.             if(timeTrans==null){  
  15.                timeTrans=new TimeTransform();  
  16.             }  
  17.             return timeTrans;  
  18.         }  
  19.         public function transDate(num:Number):String{  
  20.             date=new Date(num*1000);  
  21.             return date.fullYear+"/"+(date.month+1)+"/"+date.date;  
  22.         }  
  23.           
  24.     }  
  25. }  
 以上类,就是根据一个参数(时间戳,毫秒值),来求出今天的时间。比如游戏中的当日注册时间,因为从服务器返回的日期其实是一个时间戳(一个单位是秒的值),无法用时间戳来显示当日日期,所以此处需转换才能正确显示在界面上。