I use this script currently to determine the difference between two dates:
// 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);
The problem with this is I want to monitor the actual physical day change so if someone accessed the application at 11:59 PM and then came back 5 minutes later this would register as a 1 day difference (a new day), this current script requires atleast 12 hours to have passed between two dates for it to register as a new day.
I have thought about using the date number etc, but because months and years are so different it is quite a complex route, there must be something simpler.
As an FYI, the difference between a date and midnight of the following AM is:
// dt is the start date
var diff:Number =
new Date(dt.getYear(), dt.getMonth(), dt.getDate() + 1) - dt.getTime()
But it is easiest to simply round to the next day and then start from there:
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);
Your other option is to round the inputs:
// 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);
Something like this should work:
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;
}
even though you've accepted the answer, here's an update function:
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;
}
It works with all the test cases I've tried (midnight to 23.59.59 = 0 days, 23.59 to 00.05 = 1 day)