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 number of days between 2 date

Status
Not open for further replies.

conio96

Programmer
Dec 5, 2002
4
0
0
MY
Hi,
I have 2 string, "Feb 22, 2004" and "Mar 2, 2004". Is there any way to convert the string to date and get the day difference between these 2 string with visual c++??

Hope to hear from you soon. Thanks a lot.
 
Simpler not so efficient method would be passing the Date Strings to your database to get DateDiff() of two dates.

There are many ways to deal with this with raw VC++. You can convert them to CDate and find the difference.

Eay way to convert a string to date is Assigning the string to a Variant (vt_bstr) and change the Variant Type to vt_date.

MSDN has many examples

ALL THE BEST
 
Use COleDateTime::parseDateTime() to read the two string date into two COleDateTime objects and then do a difference.
ColeDateTime dt1 = new COleDateTime();
dt1.ParseDateTime("Feb 2, 2004",VAR_DATEVALUEONLY , LANG_USER_DEFAULT);
dt2.ParseDateTime("Mar 3, 2004",VAR_DATEVALUEONLY , LANG_USER_DEFAULT);
if (dt1.m_status == 0 && dt2.m_status == 0)
{
The two string date are valid
COleDateTimeSpan sp1 = new COleDateTimeSpan(dt1.m_dt);
COleDateTimeSpan sp2 = new COleDateTimeSpan(dt2.m_dt);
COleDateTimeSpan diff = sp1-sp2;
diff.GetDays();

}
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top