Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate duration between two dates

Status
Not open for further replies.

maximusAF

IS-IT--Management
Feb 11, 2003
6
0
0
US
I trying to calculate number of days between one date and another. I have good idea of how to do this but not sure efficient. What is the proper algorithm? How you also compute new date given one date and a number of days? Please keep algorithm simple as possible.
 
Well if your using MFC you can use COleDateTime and COleDateTimeSpan classes to calculate number of days.

If your looking a non MFC solution try the BOOST date_time class.
good luck
-pete
 
If you're making your own class, I suggest you have an array of ints twelve elements big, one for each month. In each of these elements, keep the number of days for the cooresponding month (e.g. daysInMonth[0] = 31 ).

Calculate the number of months difference between the two dates, then have a for loop run through the daysInMonth array, adding to some variable (e.g. totalDays) for each month that's passed. Once the final month is reached, add the number of days (e.g. if you're looking for the 4th, add 4).

I'm over-simplifying, but this should get you started. If using ctime, the dates are kept track of in seconds from some day in the 70s or something. It would be possible to find the days between dates by converting the difference between the two to days instead of seconds or whatever. You'd have to do a tad more research for this option, though.
 
Have you looked into the CTimeSpan class? It supports subtraction of CTime(s).

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top