# Thread: number of days between 2 dates.

1. ## number of days between 2 dates.

I want to be able to calculate the number of days between 2 dates. Ive looked at the boost c++ libararies but it isnt possible for me me to use them as I have to take my code between multiple computers.

Ive searched the forum and it returns this post:

http://cboard.cprogramming.com/showt...ber+days+dates

which talks about using difftime(); but I dont understand how that can work as Im trying to find days between dates not seconds between times

Any ideas?

2. 86400
Number of seconds in a day - what's the problem?

3. ok, i guess im being very stupid here but, both days will have the same number of second, giving me a differenence of 0 seconds

4. difftime returns the number of seconds between two times (time includes the date)

So difference between Monday and Wednesday would be 172800 seconds.

5. the thing is I have 2 dates, for example: 17 2 2005 and 14 2 2005

each part stored as individual int's. I dont see how difftime() can work for this?

6. Well you store them both in separate struct tm variables.
Then call mktime() to convert them both to time_t's
then call difftime()

I'm sure I've done this just recently - like say 5 seconds ago in another post.

7. Code:
```struct tm time_str;
struct tm time_str2;
long int diff = 0;
time_str.tm_year = 2005-1900;
time_str.tm_mon = 2-1;
time_str.tm_mday = 4;

time_str2.tm_year = 2005-1900;
time_str2.tm_mon = 2-1;
time_str2.tm_mday = 8;

diff=difftime(mktime(&time_str2),mktime(&time_str));```

i get an answer of 0. any ideas why?

8. Why does distributing your stuff to multiple computers prevent you from using Boost in any way? Distribute Boost as well, every computer with a C++ compiler should have it, anyway.

9. because some of the computers that I would need to use it on have restrictions in place so I cannot install it.

10. Try zeroing out your time structs, otherwise you will get random values in the fields you don't fill in.
Code:
```struct tm time_str  = { 0 };
struct tm time_str2 = { 0 };```

11. thanks! that fixed it