2008-10-03 134 views

回答

20

您可以將自紀元以來的兩個日期時間轉換爲毫秒,執行算術運算,然後使用最終的毫秒來計算這些更高的時間跨度數字。

var someDate:Date = new Date(...); 
var anotherDate:Date = new Date(...); 
var millisecondDifference:int = anotherDate.valueOf() - someDate.valueOf(); 
var seconds:int = millisecondDifference/1000; 
.... 

LiveDocs對於這種類型的東西也很有用。對不起,如果ActionScript是有點關閉,但它已經有一段時間了。

我還建議創建一組靜態類方法,可以執行這些操作,如果你正在做很多這種類型的數學。可悲的是,這些基本功能在標準API中並不存在。

1

沒有這樣做的自動方式。使用提供的類可以達到的最好效果是獲取date1.time和date2.time,以便從1970年1月1日起爲兩個數字提供毫秒數。然後你可以計算它們之間的毫秒數。通過一些基本的數學運算,你可以得出秒數,小時數,天數等。

1

爲了準確起見,Russell的上述文章是正確的,直到你達到25天的差異,那麼數字變得太大變量。 因此,聲明millisecondDifference:Number;

有可能是記錄的getTime()和的valueOf()之間存在一些差異,但實際上我無法看到它

26

我創建了一個時間跨度的ActionScript類具有相似的API,以System.TimeSpan填補無效,但由於缺少操作符重載,所以存在差異。您可以使用它像這樣:

TimeSpan.fromDates(later, earlier).totalDays; 

下面是類代碼(抱歉大後 - 我將不包括單元測試;)

/** 
* Represents an interval of time 
*/ 
public class TimeSpan 
{ 
    private var _totalMilliseconds : Number; 

    public function TimeSpan(milliseconds : Number) 
    { 
     _totalMilliseconds = Math.floor(milliseconds); 
    } 

    /** 
    * Gets the number of whole days 
    * 
    * @example In a TimeSpan created from TimeSpan.fromHours(25), 
    *   totalHours will be 1.04, but hours will be 1 
    * @return A number representing the number of whole days in the TimeSpan 
    */ 
    public function get days() : int 
    { 
     return int(_totalMilliseconds/MILLISECONDS_IN_DAY); 
    } 

    /** 
    * Gets the number of whole hours (excluding entire days) 
    * 
    * @example In a TimeSpan created from TimeSpan.fromMinutes(1500), 
    *   totalHours will be 25, but hours will be 1 
    * @return A number representing the number of whole hours in the TimeSpan 
    */ 
    public function get hours() : int 
    { 
     return int(_totalMilliseconds/MILLISECONDS_IN_HOUR) % 24; 
    } 

    /** 
    * Gets the number of whole minutes (excluding entire hours) 
    * 
    * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500), 
    *   totalSeconds will be 65.5, but seconds will be 5 
    * @return A number representing the number of whole minutes in the TimeSpan 
    */ 
    public function get minutes() : int 
    { 
     return int(_totalMilliseconds/MILLISECONDS_IN_MINUTE) % 60; 
    } 

    /** 
    * Gets the number of whole seconds (excluding entire minutes) 
    * 
    * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500), 
    *   totalSeconds will be 65.5, but seconds will be 5 
    * @return A number representing the number of whole seconds in the TimeSpan 
    */ 
    public function get seconds() : int 
    { 
     return int(_totalMilliseconds/MILLISECONDS_IN_SECOND) % 60; 
    } 

    /** 
    * Gets the number of whole milliseconds (excluding entire seconds) 
    * 
    * @example In a TimeSpan created from TimeSpan.fromMilliseconds(2123), 
    *   totalMilliseconds will be 2001, but milliseconds will be 123 
    * @return A number representing the number of whole milliseconds in the TimeSpan 
    */ 
    public function get milliseconds() : int 
    { 
     return int(_totalMilliseconds) % 1000; 
    } 

    /** 
    * Gets the total number of days. 
    * 
    * @example In a TimeSpan created from TimeSpan.fromHours(25), 
    *   totalHours will be 1.04, but hours will be 1 
    * @return A number representing the total number of days in the TimeSpan 
    */ 
    public function get totalDays() : Number 
    { 
     return _totalMilliseconds/MILLISECONDS_IN_DAY; 
    } 

    /** 
    * Gets the total number of hours. 
    * 
    * @example In a TimeSpan created from TimeSpan.fromMinutes(1500), 
    *   totalHours will be 25, but hours will be 1 
    * @return A number representing the total number of hours in the TimeSpan 
    */ 
    public function get totalHours() : Number 
    { 
     return _totalMilliseconds/MILLISECONDS_IN_HOUR; 
    } 

    /** 
    * Gets the total number of minutes. 
    * 
    * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500), 
    *   totalSeconds will be 65.5, but seconds will be 5 
    * @return A number representing the total number of minutes in the TimeSpan 
    */ 
    public function get totalMinutes() : Number 
    { 
     return _totalMilliseconds/MILLISECONDS_IN_MINUTE; 
    } 

    /** 
    * Gets the total number of seconds. 
    * 
    * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500), 
    *   totalSeconds will be 65.5, but seconds will be 5 
    * @return A number representing the total number of seconds in the TimeSpan 
    */ 
    public function get totalSeconds() : Number 
    { 
     return _totalMilliseconds/MILLISECONDS_IN_SECOND; 
    } 

    /** 
    * Gets the total number of milliseconds. 
    * 
    * @example In a TimeSpan created from TimeSpan.fromMilliseconds(2123), 
    *   totalMilliseconds will be 2001, but milliseconds will be 123 
    * @return A number representing the total number of milliseconds in the TimeSpan 
    */ 
    public function get totalMilliseconds() : Number 
    { 
     return _totalMilliseconds; 
    } 

    /** 
    * Adds the timespan represented by this instance to the date provided and returns a new date object. 
    * @param date The date to add the timespan to 
    * @return A new Date with the offseted time 
    */  
    public function add(date : Date) : Date 
    { 
     var ret : Date = new Date(date.time); 
     ret.milliseconds += totalMilliseconds; 

     return ret; 
    } 

    /** 
    * Creates a TimeSpan from the different between two dates 
    * 
    * Note that start can be after end, but it will result in negative values. 
    * 
    * @param start The start date of the timespan 
    * @param end The end date of the timespan 
    * @return A TimeSpan that represents the difference between the dates 
    * 
    */  
    public static function fromDates(start : Date, end : Date) : TimeSpan 
    { 
     return new TimeSpan(end.time - start.time); 
    } 

    /** 
    * Creates a TimeSpan from the specified number of milliseconds 
    * @param milliseconds The number of milliseconds in the timespan 
    * @return A TimeSpan that represents the specified value 
    */  
    public static function fromMilliseconds(milliseconds : Number) : TimeSpan 
    { 
     return new TimeSpan(milliseconds); 
    } 

    /** 
    * Creates a TimeSpan from the specified number of seconds 
    * @param seconds The number of seconds in the timespan 
    * @return A TimeSpan that represents the specified value 
    */ 
    public static function fromSeconds(seconds : Number) : TimeSpan 
    { 
     return new TimeSpan(seconds * MILLISECONDS_IN_SECOND); 
    } 

    /** 
    * Creates a TimeSpan from the specified number of minutes 
    * @param minutes The number of minutes in the timespan 
    * @return A TimeSpan that represents the specified value 
    */ 
    public static function fromMinutes(minutes : Number) : TimeSpan 
    { 
     return new TimeSpan(minutes * MILLISECONDS_IN_MINUTE); 
    } 

    /** 
    * Creates a TimeSpan from the specified number of hours 
    * @param hours The number of hours in the timespan 
    * @return A TimeSpan that represents the specified value 
    */ 
    public static function fromHours(hours : Number) : TimeSpan 
    { 
     return new TimeSpan(hours * MILLISECONDS_IN_HOUR); 
    } 

    /** 
    * Creates a TimeSpan from the specified number of days 
    * @param days The number of days in the timespan 
    * @return A TimeSpan that represents the specified value 
    */ 
    public static function fromDays(days : Number) : TimeSpan 
    { 
     return new TimeSpan(days * MILLISECONDS_IN_DAY); 
    } 

    /** 
    * The number of milliseconds in one day 
    */ 
    public static const MILLISECONDS_IN_DAY : Number = 86400000; 

    /** 
    * The number of milliseconds in one hour 
    */ 
    public static const MILLISECONDS_IN_HOUR : Number = 3600000; 

    /** 
    * The number of milliseconds in one minute 
    */ 
    public static const MILLISECONDS_IN_MINUTE : Number = 60000; 

    /** 
    * The number of milliseconds in one second 
    */ 
    public static const MILLISECONDS_IN_SECOND : Number = 1000; 
} 
+1

@nchrysler - 我不建議添加新的特性別人的示例代碼。儘管如此,隨意添加您自己的答案與新功能。 – 2012-01-22 14:29:00

+0

@Erwinus - 有趣的,你能給我一個repro聲明嗎? – 2012-12-27 03:59:05

+0

你可以添加一個許可證嗎?最好是Apache或BSD?不幸的是,有些人在沒有許可證的情況下不能使用它。 – 2015-07-26 22:30:26

0

ArgumentValidation是另一類Szalays先生做了一些檢查,以確保每種方法都有正確的價值來執行它的任務而不會導致無法識別的錯誤。它們對於TimeSpan類的工作非常重要,因此您可以將它們註釋掉,並且類將正常工作。

豐富的可發佈參數驗證類在這裏也因爲它是非常方便的,但我會留下下來給他,P

4

對於這樣的一個單一的功能,我更可取... [理查德·紹洛伊的代碼凝聚]

public function timeDifference(startTime:Date, endTime:Date) : String 
{ 
if (startTime == null) { return "startTime empty."; } 
if (endTime == null) { return "endTime empty."; } 
var aTms = Math.floor(endTime.valueOf() - startTime.valueOf()); 
return "Time taken: " 
    + String(int(aTms/(24*60*+60*1000)) ) + " days, " 
    + String(int(aTms/( 60*60*1000)) %24) + " hours, " 
    + String(int(aTms/(  60*1000)) %60) + " minutes, " 
    + String(int(aTms/(  1*1000)) %60) + " seconds."; 
} 
1
var timeDiff:Number = endDate - startDate; 
var days:Number  = timeDiff/(24*60*60*1000); 
var rem:Number  = int(timeDiff % (24*60*60*1000)); 
var hours:Number = int(rem/(60*60*1000)); 
rem     = int(rem % (60*60*1000)); 
var minutes:Number = int(rem/(60*1000)); 
rem     = int(rem % (60*1000)); 
var seconds:Number = int(rem/1000); 

trace(days + " << >> " +hours+ " << >> " +minutes+ " << >> " +seconds); 

var time:Number = targetDate - currentDate; 
var secs:Number = time/1000; 
var mins:Number = secs/60; 
var hrs:Number = mins/60; 
var days:Number = int(hrs/24); 

secs = int(secs % 60); 
mins = int(mins % 60); 
hrs = int(hrs % 24); 

trace(secs + " << >> " + mins + " << >> " + hrs + " << >> " + days); 
相關問題