2011-09-01 74 views
1

我使用這個腳本目前確定兩個日期之間的差值之間計算天:AS2:兩個日期

// 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個小時才能在兩個日期之間通過,作爲新的一天註冊。

我曾考慮過使用日期編號等,但因爲幾個月和幾年如此不同,它是相當複雜的路線,必須有一些更簡單的方法。

+0

什麼是來自滑塊的值? – grapefrukt

+0

@grapefrukt毫秒from getTime()' –

回答

1

作爲一個供參考,以下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); 
+0

這種做法有什麼不同?它仍然通過在一天中除以毫秒和四捨五入來計算日期差異,這意味着它仍然需要半天才能通過否。 –

+0

它將兩天的時間縮短到最近的一天(或在第二個示例中爲倒數),這意味着如果是9月1日23:59,它將到9月2日0:00爲止,如果是9月1日0:01。 2它會輪到0:00 9月3日 - 一天差 – cwallenpoole

+0

啊好吧我現在明白了,謝謝 –

0

像這樣的東西應該工作:

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天)

+0

但是我想記錄自他們上次使用該工具以來的天數,所以如果他們在兩個月後回來。然後我遇到了幾個月/閏年等可變數量的問題 –