Thread: Help
View Single Post
Old Jan 28th, 2008, 7:30 PM   #8
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Help

Another way to do it is to just convert both dates to days and subtract the two. Something like this.
unsigned int day1 = 1, month1 = 1, year1 = 2007;
unsigned int day2 = 1, month2 = 1, year2 = 2008;
unsigned int days1 = year1*365 + day1;

if( month1 > 1)
// we do not want to include the number of days in the 
// current month, so we have to subtract 2.
    days1 += DaysInYear[month1-2];

unsigned int days2 = year1*365  + day1;
if( month2 > 1)
    days1 += DaysInYear[month2-2];

unsigned int delta = days2 - days1;

The above is not 100% accurate because it does not account for leap years. So I would create a loop to run from year 0 to year1 and from 0 to year2 and add 1 for each leap year found. There are probably faster ways to do it, but that will do.
Ancient Dragon is offline   Reply With Quote