# Thread: Calculating days between two Dates

1. ## Calculating days between two Dates

Anyone one of you can think of a clever algorithm?

The date input format should be ( dd, mm, yyyy ). All I can think of is that to work out the number of months between the two date. And then work out the number of days for each month, And lastly add up all the days. But this is a pain in the s.

Since leap year is taken into concern, the calculation is quite tedious (although not impossible). I am thinking of the possiblity of a shorter algorithm. It nice if you guys have some suggestions.

Thanks a lot.

2. No one knows a better method?

http://www.cprogramming.com/cboard/a...p?s=&forumid=3

4. sounds like a reasonable method for working it out, not too hard either

http://www.cprogramming.com/cboard/a...p?s=&forumid=3
oh, sorry... So do you have any idea about calculation in mind? I am now thinking whether the famous "Zeller's algorithm" will help. But seems unrelated...

6. Originally posted by Yin

oh, sorry... So do you have any idea about calculation in mind? I am now thinking whether the famous "Zeller's algorithm" will help. But seems unrelated...
well, It should be me who say sorry. zeller is for calculating the day of a date (ie. sunday, monday, etc) isn't it? How can it help??

7. Look into the time header file. Within there you will find several time funtions including difftime(). difftime() will give you the number of seconds between two different dates/time returned as type long. This can be directly translated into the number of days between two dates (divide by the product of 24 * 60 * 60. You can then divide 365 to give an approximation to the number of years. And then the remainder by 30 to get an approximation to the number of months with the remainder of that being the number of days. If you need EXACT number of months and years, then you will need to dig further in time header file than I have dug, or create the function yourself. One of the drawbacks to the time.h file is many of the functions use a struct called tm to hold data, or use type time_t or pointer to type time_t which is a bit of a nuisance until you get used to it.

8. Originally posted by Unregistered
Look into the time header file. Within there you will find several time funtions including difftime(). difftime() will give you the number of seconds between two different dates/time returned as type long. This can be directly translated into the number of days between two dates (divide by the product of 24 * 60 * 60. You can then divide 365 to give an approximation to the number of years. And then the remainder by 30 to get an approximation to the number of months with the remainder of that being the number of days. If you need EXACT number of months and years, then you will need to dig further in time header file than I have dug, or create the function yourself. One of the drawbacks to the time.h file is many of the functions use a struct called tm to hold data, or use type time_t or pointer to type time_t which is a bit of a nuisance until you get used to it.
Thanks. But I only need to calculate the number of "days" difference between two date. No need to get the month or year different. For example, given 11, 3, 2002 and 9, 3, 2002 (dd mm yyyy), the date difference output should be 2.
And it needs to be exact too. Probelm arise when the two input are of two different year. I need to find out whehter it is leap year or not. Then need to find out all the day within EACH month. That is so tedious.

9. If you use difftime() you don't need to know about leap year. difftime takes that into account as it is counting up the seconds that have occurred between the two date/times. Just divide the double returned by difftime() by the number of seconds in a day to get the number of days. The trick, if there is one, is to convert the input format into a format that can be used by difftime(). Again I refer you to the time header file to get that information.

10. oh, Thanks. But using the timediff() seems a bit lazy. hoho.
Also, reducing my input date to "seconds" seems a bit .... over-doing something. Anyway, if timediff() would return the number of days(integer), that would be best.