Working out days between 2 dates

robert.mccarthyrobert.mccarthy Member Posts: 165
edited May 2012 in Working with GS (Mac)
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,

Best Answer

  • RThurmanRThurman Posts: 2,881
    Accepted Answer
    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

  • robert.mccarthyrobert.mccarthy Member Posts: 165
    Sorry to do this, but it has been about a day so going to 'bump' up to see if anyone else can help :S
  • simo103simo103 Member, PRO Posts: 1,331
    @robert.mccarthy ... yes turns out that isn't so easy because months have varying lengths. I previously was able to find a way to determine what day of the week it was and have a template demo for that .. perhaps in that coding you can find a way to express each date as a number that can be compared. I recall it having some clever adjustments depending on the month (I found the logic somewhere online so I can't take any credit). How this helps ... look for Days of Week demo here:
    http://www.funkymunkygames.com/GameSalad/search/
  • robert.mccarthyrobert.mccarthy Member Posts: 165
    Thanks, i'll have a look.

    While I look, does anyone else have any comments/suggestions/ideas.

    Thanks,
  • robert.mccarthyrobert.mccarthy Member Posts: 165
    Will this help work out leap years etc?

    Is this where the rounding down comes in? i.e. when its a leap year, it will not be a decimal?
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    edited June 2012
    Don't know if it works for leap years -- but I assume it does. I think the leap year fix comes from making January and February the 13th and 14th month.

    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.
Sign In or Register to comment.