difference of dates

A

Alex

I need to calculate the number of days between two dates formatted yyyymmdd.
Besides creating a whole date class, is there a simpler method of doing it?

for example:
20050203 - 20050201 = 2

right now what I have is
string sdate1 = "20050203";
string sdate2 = "20050201";
string date = date1 .substr(6.2);
int ndate1 = atoi(sdate1.c_str());
date = date2 .substr(6.2);
int ndate2 = atoi(sdate2.c_str());
//calculate how many days are missing
days = ndate1 - ndate2 ;

which just gets me the difference between dates without taking into account
month and year and leap year. Any suggestions would be appreciated.

Thanks!
 
V

Victor Bazarov

Alex said:
I need to calculate the number of days between two dates formatted
yyyymmdd. Besides creating a whole date class, is there a simpler
method of doing it?
for example:
20050203 - 20050201 = 2

right now what I have is
string sdate1 = "20050203";
string sdate2 = "20050201";
string date = date1 .substr(6.2);
int ndate1 = atoi(sdate1.c_str());
date = date2 .substr(6.2);
int ndate2 = atoi(sdate2.c_str());
//calculate how many days are missing
days = ndate1 - ndate2 ;

which just gets me the difference between dates without taking into
account month and year and leap year. Any suggestions would be
appreciated.

Calculate Julian Date from year/month/day and then compare the two.
Search the web for Julian Date calculation.

V
 
J

James Aguilar

Use the header <ctime> and struct tm. Fill out the struct, then use the
functions provided in the header.

- JFA1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top