Thread: Getting the difference between dates

1. Getting the difference between dates

Hello again. What I need to do this time is get a date from the user and then compare it to the current date, to see if there are more than (int days) inbetween those two dates. I looked up a few bits in Google, but I only found a function using strptime which isn't in my time.h.

Any ideas that would get me going? I have a procedure in my mind that could do it (convert both dates into days by month * 28, month * 29, month * 30, month * 31 respectively) but it's really, really lengthy and I want to avoid it.

2. difftime is ANSI function

3. Code:
`scanf("&#37;d/%d/%d", &soldiers.dateout.dd, &soldiers.dateout.mm, &soldiers.dateout.yyyy);`
Code:
```int comparedates(struct sdate dateout)
{
time_t curtime;
struct tm tdateout;

time(&curtime);
tdateout.tm_mday = dateout.dd;
tdateout.tm_mon = dateout.mm;
tdateout.tm_year = dateout.yyyy - 1900;

if (difftime(tdateout, curtime) < 3888000) return -1;
else return 1;
}```
Could this work to find if the difference between the current date and the entered date is smaller or not than 45 days?

4. difftime takes two time_t objects, so you'll have to convert tdateout to a time_t using mktime(). It might not hurt to initialize some of the other parts of tdateout also (since we're comparing down to the second, either setting the time to midnight or the current time).

5. Yeah, you're right on that last part, I shouldn't just have guessed that it initializes to 12:00am.

6. Originally Posted by Leftos
Hello again. What I need to do this time is get a date from the user and then compare it to the current date, to see if there are more than (int days) inbetween those two dates. I looked up a few bits in Google, but I only found a function using strptime which isn't in my time.h.

Any ideas that would get me going? I have a procedure in my mind that could do it (convert both dates into days by month * 28, month * 29, month * 30, month * 31 respectively) but it's really, really lengthy and I want to avoid it.
Hi!!
I once wrote a program to do that!!
I converted it from the type of really lengthy program you describe.
I think I did it in about 5 lines in the end!!
I would be interested to see if anyone else can do it in about the same number of lines.
It too me a while to figure out how to do it but it defiinately can be done.
I can't remember exactly how I did it at the moment (well not enough to write the program right now anyway).
Basically you added 365 for each year and then 1 for each leap year then you just have to take care of the months which you assume are 30 then use an offset for a particular month, so you would have a table containing an offset for each month such as
( 1,-2,1,0,1,0,1,1,0,1,0,1). So you just did the difference in years X 365 then difference in months times 30 then, then you added your offsets which is dead easy for the months, bit harder for the leap years which are one on every 4 normally but there are differences if the dates are divisible by 400, or something like that.
You also might get problems with various people, Popes, adding or taking away a few days here and there.
Not sure how how difftime does it, but I expect my method is faster

I will have a go at doing it again sometime and see if it is faster
Bound to be, one would imagine

7. I think I did it in about 5 lines in the end!!
I would be interested to see if anyone else can do it in about the same number of lines.
Hey Esbo, would this qualify?

Code:
```#include <stdio.h>

unsigned long CalcDays(int iMonth, int iDay, int iYear)
{
return (iDay + (153 * (iMonth + 12 * ((14 - iMonth) / 12) - 3) + 2) / 5 + 365 *
(iYear + 4800 - ((14 - iMonth) / 12)) + (iYear + 4800 - ((14 - iMonth) / 12)) / 4 - 32083);
}

int main(void)
{
printf("%d\n", CalcDays(1, 17, 2008) - CalcDays(1, 03, 2008) );
return 0;
}```

8. Originally Posted by BobS0327
Hey Esbo, would this qualify?

Code:
```#include <stdio.h>

unsigned long CalcDays(int iMonth, int iDay, int iYear)
{
return (iDay + (153 * (iMonth + 12 * ((14 - iMonth) / 12) - 3) + 2) / 5 + 365 *
(iYear + 4800 - ((14 - iMonth) / 12)) + (iYear + 4800 - ((14 - iMonth) / 12)) / 4 - 32083);
}

int main(void)
{
printf("%d\n", CalcDays(1, 17, 2008) - CalcDays(1, 03, 2008) );
return 0;
}```
Hi Bob!
I am not sure yet as I can't understand it yet, will it work over a period of several years?
Using the same year and month is not a very good test as that is the tricky bit.
I will have a better look at it later and see if I can recreate my old program too.