【问题标题】:AS2: Calculate days between two datesAS2:计算两个日期之间的天数
【发布时间】:2011-11-08 06:52:15
【问题描述】:

我目前使用这个脚本来确定两个日期之间的差异:

// Slide_Tracker[?].date_int are results from the built in function getTime()
var current_date = new Date(Slide_Tracker[i].date_int);
var past_date:Date = new Date(Slide_Tracker[i - 1].date_int);
var date_diff:Number = Math.round((current_date - past_date) / 86400000);

问题是我想监控实际的物理日变化,所以如果有人在晚上 11:59 访问应用程序,然后在 5 分钟后返回,这将记录为 1 天的差异(新的一天),这当前脚本需要在两个日期之间至少经过 12 小时才能注册为新的一天。

我曾考虑过使用日期数字等,但由于月份和年份的不同,这是一条相当复杂的路线,必须有更简单的方法。

【问题讨论】:

  • 滑块输入的值是什么?
  • @grapefrukt 毫秒从getTime()

标签: flash actionscript


【解决方案1】:

这样的事情应该可以工作:

public boolean isNewDay( current:Date, past:Date ):Boolean
{
    // check the days of the month first
    if( current.date != past.date )
        return true;

    // check the months in case they came back on the same day of the next month
    if( current.month != past.month )
        return true;

    // finally check the year, in case they came back on the same day the next year
    if( current.fullYear != past.fullYear )
        return true;

    return false;
}

即使你已经接受了答案,这里有一个更新函数:

public function getNumberOfDays( current:Date, past:Date ):int
{
    // get the number of millis between the two dates
    var millis:Number = current.time - past.time;

    // a day in millis is 1000 (s) * 60 (m) * 60 (h) * 24 (day)
    var day:Number = 1000 * 60 * 60 * 24;

    // get the number of days
    var numDays:int = int( millis / day );

    // create midnight of the current day
    if ( numDays == 0 )
    {
        // if our numDays is 0, check if the current date is after midnight and the
        // previous date was before midnight the previous day, in which case, count
        // it as another day
        var midnight:Date = new Date( current.fullYear, current.month, current.date );
        if ( current.time > midnight.time && past.time < midnight.time )
            numDays++;
    }

    return numDays;
}

它适用于我尝试过的所有测试用例(午夜到 23.59.59 = 0 天,23.59 到 00.05 = 1 天)

【讨论】:

  • 但是我想记录他们自上次使用该工具以来的天数,以便他们在两个月后回来。然后我遇到了诸如月/闰年的可变天数等问题。等
【解决方案2】:

作为仅供参考,以下上午的日期和午夜之间的差异是:

// dt is the start date
var diff:Number = 
      new Date(dt.getYear(), dt.getMonth(), dt.getDate() + 1) - dt.getTime()

但最简单的方法是简单地四舍五入到第二天然后从那里开始:

var dt:Date = new Date(Slide_Tracker[i - 1].date_int);
var past_date = // start at the next day to only deal w/ 24 hour increments
    new Date(dt.getYear(), dt.getMonth(), dt.getDate() + 1);
dt = new Date(Slide_Tracker[i].date_int);
var current_date = 
    new Date(dt.getYear(), dt.getMonth(), dt.getDate() + 1);
var date_diff:Number = Math.round((current_date.getTime() - 
                                   past_date.getTime()) / 86400000);

您的另一个选择是对输入进行四舍五入:

// rounds a timestamp *down* to the current day
function getBaseDay(val:Number):Number
{
    return Math.floor( val / 86400000 ) * 86400000
}

var current_date = new Date(getBaseDay(Slide_Tracker[i].date_int));
var past_date:Date = new Date(getBaseDay(Slide_Tracker[i - 1].date_int));
var date_diff:Number = Math.round((current_date.getTime() - 
                                   past_date.getTime()) / 86400000);

【讨论】:

  • 这有什么不同?它仍然通过除以一天中的毫秒数和四舍五入来计算日期的差异,这意味着它仍然需要半天时间才能通过吗?
  • 它会将两天向上取整到最近的一天(或在第二个示例中向下取整),这意味着如果是 9 月 1 日 23:59,它将四舍五入到 9 月 2 日 0:00,如果现在是 9 月 2 日 0:01,它将舍入到 9 月 3 日 0:00 -- 相差一天
猜你喜欢
  • 1970-01-01
  • 2015-02-26
  • 2019-09-27
  • 1970-01-01
  • 2017-07-22
相关资源
最近更新 更多