# Thread: Cannot calculate the difference between two dates

1. ## Cannot calculate the difference between two dates

I'm trying to calculate the difference between two dates (the current date and a date stored in a structure) expressed in hours, minutes and seconds, but when I print the results, it's not being calculated correctly.

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

int main(void) {
struct tm date;
date.tm_year = 2019 - 1900;
date.tm_mon = 1 - 1;
date.tm_mday = 8;
date.tm_hour = 2;
date.tm_min = 30;
date.tm_sec = 0;
time_t PreviousDate = mktime(&date);
time_t CurrentDate = time(NULL);
time_t difference = difftime(CurrentDate, PreviousDate);
time_t hours, minutes;
hours = difference / (60*60);
difference -= difference % (60*60);
minutes = difference / 60;
difference -= difference % 60;
printf("%ld hours, %ld minutes and %ld seconds.\n", hours, minutes, difference);
return 0;
}```
This is what's being shown:
Code:
`19 hours, 1140 minutes and 68400 seconds.`
Which is not right, because the maximum of minutes and seconds should be 60. It seems the remainder of the conversion to minutes is not working, because 1140 minutes are 19 hours and 68400 seconds are 19 hours.

Could it be that the type time_t can't be operated with integers? If it can't, how should I calculate the difference between those dates? 2. Your main problem is an incorrect calculation.
This is wrong:
Code:
`difference -= difference % (60*60);`
You want something a little simpler:
Code:
`difference %= (60*60);`
Same thing with calculating minutes.

And it's best to make difference a long so that you know exactly what it is (time_t could be anything).
difftime returns a double, but if you aren't interested in the fractional seconds, storing it's return value in a long is good enough.
Code:
```#include <stdio.h>
#include <time.h>

int main() {
struct tm date;
date.tm_year = 2019 - 1900;
date.tm_mon = 1 - 1;
date.tm_mday = 8;
date.tm_hour = 2;
date.tm_min = 30;
date.tm_sec = 0;
date.tm_isdst = -1; // ask system to try to determine if dst is true or not

time_t PreviousDate = mktime(&date);
time_t CurrentDate = time(NULL);
long difference = difftime(CurrentDate, PreviousDate);

int hours = difference / (60 * 60);
difference %= (60 * 60);
int minutes = difference / 60;
difference %= 60;
printf("%d hours, %d minutes and %ld seconds.\n", hours, minutes, difference);

return 0;
}``` 3. Originally Posted by john.c Your main problem is an incorrect calculation.
This is wrong:
Code:
`difference -= difference % (60*60);`
You want something a little simpler:
Code:
`difference %= (60*60);`
Same thing with calculating minutes.
[/code]
Whoops! I didn't notice it. I understand why the result was always 19 hours now. It's because I was subtracting the remainder, so I was getting the quotient. But with difference %= (60*60); I subtract the quotient to get the remainder, which is the expected result. Originally Posted by john.c And it's best to make difference a long so that you know exactly what it is (time_t could be anything).
difftime returns a double, but if you aren't interested in the fractional seconds, storing it's return value in a long is good enough.
Okay, I'll keep this in mind.

Thank you very much, john.c. Popular pages Recent additions difference, hours, minutes, seconds, time_t 