5 Replies Latest reply on Sep 20, 2006 7:50 AM by kglad

# Days Since

I'm looking to calculate the days since a date and the number of days until a date.

Basically I tried getting todays date and subtracting the date(in the past) to get the difference in days/months/years. I was then thinking of converting the numbers to ratios of days, so 1 year=365 dats and then totalling the days/months/years since.

Did that make sense? I hope so?

But then I had an anti-Eureka moment. I realised that months have different numbers of days (Especially february-grrr!) so I was wondering if there was a better way of working out the number of days since/to.
• ###### 1. Re: Days Since
var date1:Date = new Date();
var date2:Date = new Date(2006,5,21);
var date3:Date = new Date(date2.valueOf() - date1.valueOf());

trace(date1);
trace(date2);
trace(date3);

var ms = Math.abs(date3.valueOf());
var secs = ms/1000;
var mins = secs/60;
var hrs = mins/60;
var days = hrs/24;
trace(" milliseconds since " + date2 + " " + ms);
trace(" seconds since " + date2 + " " + secs);
trace(" minutes since " + date2 + " " + mins);
trace(" hours since " + date2 + " " + hrs);
trace(" days since " + date2 + " " + days);

• ###### 2. Days Since
you can simply subtract the dates from eachother

eg.

a = new Date();
b = new Date(2006, 8, 20, 13, 20, 00, 00);
trace((a-b)/1000/60); // the returned result of a-b is in milliseconds, so dividing it by 60000 will give you minutes - but watch out: 7.5 min is 7:30, and 7.99 is 7' 59" and 4 ms

let me know how it works ;)

cheers

edit: i just noticed that you need days, so what you have to do is to divide the returned result by 1000/60/60/24
eg:

(a-b)/1000/60/60/24
• ###### 3. Re: Days Since
Great, thanks, I'm looking forward to trying that. Unfortunately my trace is broken...

Nothing's tracing..even trace("Hello") doesn't work?

Doh :(
• ###### 4. Re: Days Since
quote:

Originally posted by: pall zoltan
you can simply subtract the dates from eachother

eg.

a = new Date();
b = new Date(2006, 8, 20, 13, 20, 00, 00);
trace((a-b)/1000/60); // the returned result of a-b is in milliseconds, so dividing it by 60000 will give you minutes - but watch out: 7.5 min is 7:30, and 7.99 is 7' 59" and 4 ms

let me know how it works ;)

cheers

edit: i just noticed that you need days, so what you have to do is to divide the returned result by 1000/60/60/24
eg:

(a-b)/1000/60/60/24

Thanks for that, it works a treat. Although I'd rather forget seconds and minutes altogether. Is there a way of making a and b so that they return an integer of the nearest number of days (i.e formatting a subtraction so that only days months and years are included)?
• ###### 5. Re: Days Since
use Math.floor: