Working out days between 2 dates
Hi All,
Within my app, I have 2 dates (date, month, year), and I need to work out the difference between them in days.
Has anyone done this before, and can point me in the right direction.
I have been looking into the following:
Attributes: New Date
Attributes: Old Date
Attributes: Days Difference
To work out if a year has passed to add 365 days:
If (old month >= new month) && (old day >= new day) && (old year >=new day) then add +356 to my days difference attribute.
To work out the number of days passed:
new days - old days = xxdays added to days difference attribute.
This is where I am stuck, or am I going about this the wrong way??
All and any help appreciated.
Thanks,
Within my app, I have 2 dates (date, month, year), and I need to work out the difference between them in days.
Has anyone done this before, and can point me in the right direction.
I have been looking into the following:
Attributes: New Date
Attributes: Old Date
Attributes: Days Difference
To work out if a year has passed to add 365 days:
If (old month >= new month) && (old day >= new day) && (old year >=new day) then add +356 to my days difference attribute.
To work out the number of days passed:
new days - old days = xxdays added to days difference attribute.
This is where I am stuck, or am I going about this the wrong way??
All and any help appreciated.
Thanks,
Best Answer
-
RThurman Posts: 2,881
The website < http://mathforum.org/library/drmath/view/66857.html > has the following instructions:
First of all, since February is an especially short month, it is
normally better to consider January and February the 13th and 14th
months of the previous year. So first, if the month is 1 or 2, then
you add 12 to the month and subtract 1 from the year. Then the day is
365*year + year/4 - year/100 + year/400 + date + (153*month+8)/5
where all of the divisions are rounded DOWN to the nearest integer.
Do this for both dates, and subtract.
Answers
http://www.funkymunkygames.com/GameSalad/search/
While I look, does anyone else have any comments/suggestions/ideas.
Thanks,
Is this where the rounding down comes in? i.e. when its a leap year, it will not be a decimal?
Give it a try and let us know!
Edit:
Dang! GameSalad is just too addicting. I couldn't stop thinking about it so I tested it out. The algorithm does seem to work. I used GS to test the difference between Feb.15 and March 15, 2011 and got 28 days. Then I tested the difference between the same days in 2012 (a leap year) and got 29 days.