Flash AS2:计算两个日期之间的天数

Flash AS2:计算两个日期之间的天数,flash,actionscript,Flash,Actionscript,我当前使用此脚本来确定两个日期之间的差异: // 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.roun

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

// 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小时才能注册为新的一天


我曾考虑过使用日期编号等,但由于月份和年份的差异非常大,这是一条非常复杂的路线,因此必须有更简单的方法。

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

// 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);

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

// 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);

像这样的方法应该会奏效:

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;
}
公共函数getNumberOfDays(当前:日期,过去:日期):int { //获取两个日期之间的毫秒数 var millis:Number=current.time-pass.time; //以毫秒为单位的一天是1000(s)*60(m)*60(h)*24(天) 变量日:数值=1000*60*60*24; //获取天数 变量numDays:int=int(毫秒/天); //创建当前日期的午夜 如果(numDays==0) { //如果我们的numDays为0,请检查当前日期是否在午夜之后,以及 //前一个日期是前一天的午夜之前,在这种情况下,计数 //这是另一天 var午夜:日期=新日期(current.fullYear、current.month、current.Date); 如果(当前时间>午夜时间&过去时间<午夜时间) numDays++; } 返回天数; }
它适用于我尝试过的所有测试用例(午夜到23.59.59=0天,23.59到00.05=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;
}
公共函数getNumberOfDays(当前:日期,过去:日期):int { //获取两个日期之间的毫秒数 var millis:Number=current.time-pass.time; //以毫秒为单位的一天是1000(s)*60(m)*60(h)*24(天) 变量日:数值=1000*60*60*24; //获取天数 变量numDays:int=int(毫秒/天); //创建当前日期的午夜 如果(numDays==0) { //如果我们的numDays为0,请检查当前日期是否在午夜之后,以及 //前一个日期是前一天的午夜之前,在这种情况下,计数 //这是另一天 var午夜:日期=新日期(current.fullYear、current.month、current.Date); 如果(当前时间>午夜时间&过去时间<午夜时间) numDays++; } 返回天数; }
它适用于我尝试过的所有测试用例(午夜到23.59.59=0天,23.59到00.05=1天)

滑块中的值是多少?
getTime()中的@grapebrukt毫秒
滑块中的值是多少?
getTime()中的@grapebrukt毫秒)
但我想记录他们上次使用该工具的天数,如果他们两个月后回来。然后我遇到了一些问题,比如月份/闰年中的天数可变。等等,但我想记录他们上次使用该工具以来的天数,如果他们两个月后回来的话。然后我遇到了一些问题,比如月/闰年天数的变化。等等,这有什么不同?它仍然通过除以一天中的毫秒数和四舍五入来计算日期差,这意味着它仍然需要半天才能通过。否?它将两天向上舍入到最近的一天(或在第二个示例中向下舍入),这意味着如果是9月1日23:59,它将在9月2日的0:00结束,如果是9月2日的0:01,它将在9月3日的0:00结束——一天不同这有什么不同?它仍然通过除以一天中的毫秒数和四舍五入来计算日期差,这意味着它仍然需要半天才能通过。否?它将两天向上舍入到最近的一天(或在第二个示例中向下舍入),这意味着如果是9月1日23:59,它将在9月2日的0:00左右结束,如果是9月2日的0:01,它将在9月3日的0:00左右结束——相差一天