By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

difference of dates

P: n/a
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!
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alex wrote:
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
Jul 23 '05 #2

P: n/a
Use the header <ctime> and struct tm. Fill out the struct, then use the
functions provided in the header.

- JFA1
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.