ActionScript 3.0 + 计算两个日期之间的时间跨度?

发布于 2024-07-05 23:11:58 字数 132 浏览 10 评论 0原文

在ActionScript 3.0中,是否有一种自动方法来计算两个指定日期之间的天数、小时数、分钟数和秒数?

基本上,我需要的是 .NET Timespan 类的 ActionScript 等效项。

任何想法?

In ActionScript 3.0, is there an automatic way to calculate the number of days, hours, minutes and seconds between two specified dates?

Basicly, what I need is the ActionScript equivalent of the .NET Timespan class.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无人接听 2024-07-12 23:11:58

我创建了一个 ActionScript TimeSpan 类,它具有与 System.TimeSpan 类似的 API 来填补这一空白,但由于缺乏运算符重载,因此存在差异。 您可以像这样使用它:

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;
}

I created an ActionScript TimeSpan class with a similar API to System.TimeSpan to fill that void, but there are differences due to the lack of operator overloading. You can use it like so:

TimeSpan.fromDates(later, earlier).totalDays;

Below is the code for the class (sorry for the big post - I won't include the Unit Tests ;)

/**
 * 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;
}
一影成城 2024-07-12 23:11:58

您可以将两个日期时间转换为自纪元以来的毫秒数,执行数学运算,然后使用所得的毫秒数来计算这些更高的时间跨度数字。

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 中并不真正存在这种基本功能。

You can covert the two date times into milliseconds since the epoch, perform your math and then use the resultant milliseconds to calculate these higher timespan numbers.

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

The LiveDocs are useful for this type of thing too. Sorry if the ActionScript is a bit off, but it has been a while.

I'd also recommend creating a set of static class methods that can perform these operations if you're doing a lot of this type of math. Sadly, this basic functionality doesn't really exist in the standard APIs.

温柔嚣张 2024-07-12 23:11:58

对于某些像这样的单一功能,我最好......
[摘自 Richard Szalay 的代码]

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.";
}

for some a single function like this my be preferable...
[condensed from Richard Szalay's code]

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.";
}
萌吟 2024-07-12 23:11:58

没有自动的方法可以做到这一点。 使用提供的类可以实现的最佳效果是获取 date1.time 和 date2.time,以给出两个数字自 1970 年 1 月 1 日以来的毫秒数。 然后您可以计算出它们之间的毫秒数。 通过一些基本的数学,你可以推导出秒、小时、天等。

There is no automatic way of doing this. The best you can achieve with the supplied classes is to fetch date1.time and date2.time, to give the number of milliseconds since 1st Jan 1970 for two numbers. You can then work out the number of milliseconds between them. With some basic maths, you can then derive the seconds, hours, days etc.

晨与橙与城 2024-07-12 23:11:58

为了准确起见,Russell 的上述帖子是正确的,直到您达到 25 天的差异,然后该数字对于 int 变量来说太大了。
因此声明 millisecondDifference:Number;

记录的 getTime() 和 valueOf() 之间可能存在一些差异,但实际上我看不到它

For the sake of accuracy the above post by Russell is correct until you get to 25 days difference, then the number becomes too large for the int variable.
Therefore declare the millisecondDifference:Number;

There may be some difference between the documented getTime() and valueOf(), but in effect I can't see it

2024-07-12 23:11:58
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);
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);

or

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);
女皇必胜 2024-07-12 23:11:58

ArgumentValidation 是 Mr Szalays 的另一个类,它会进行一些检查,以确保每个方法都有正确的值来执行其任务,而不会引发无法识别的错误。 它们对于让 TimeSpan 类工作并不是必需的,因此您只需将它们注释掉,该类就会正常工作。

Rich 可能会在这里发布参数验证类,这非常方便,但我会把它留给他;P

ArgumentValidation is another class of Mr Szalays that does some checks to make sure each method has the right values to perform it's tasks without throwing unrecognisable errors. They are non-essential to get the TimeSpan class working so you could just comment them out and the class will work correctly.

Rich may post the Argument validation class on here as well as it's very handy but i'll leave that down to him ;P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文