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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date in VC++

Status
Not open for further replies.

kathyayini

Programmer
Aug 16, 2002
52
IN
I wants to check whether given date is valid or not or given date is greater/less than current date or i wants to add some date to given date.
Is there is any direct functions for all the above queries.
Waiting for the reply.
 
If you're using MFC then use the CTime and CTimeSpan classes - you can compare two date/times using the comparison operators '<' and '>', etc.

If you're using ANSI style C/C++, then you can use the [tt]difftime()[/tt] function to get the time difference between two [tt]time_t[/tt] structures:

[tt]time_t nowTime;
time(&nowTime);
gmtime(&nowTime); // get the current GMT time

// assume 'oldTime' is some older time variable
// already initialized
double diff = difftime(nowTime,oldTime);[/tt]

actually, a time_t value is simply the number of seconds since jan 1st, 1970. You can get the difference between two time_t values in seconds like this:

[tt]unsigned diff = (unsigned)t1-(unsigned)t2;[/tt]

where t1 is a later time than t2. Also you can compare them:

[tt]if ((unsigned)t1 > (unsigned)t2) {}[/tt]

:)
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
earlier i was using CTime but year must be with in 1970–2038 so i dropped the idea of using CTime and CTimeSpan. and difftime returns double-precision floating-point number.
but i want date/month/year difference.
 
for getting the current date and time u can use the function
GetSystemTime(&sysTime);
sysTime is of type SYSTEMTIME structure.

GetSystemTime() fills the SYSTEMTIME structure with the
current date and time.

Now u can use the individual elements of this structure to
compare the current date with the given date.

Hope this helps u.
 
COleDateTime is similar to CTime but has a much larger time range. However, I'm not too sure if the CTimeSpan class will work past 1970 - you could give it a shot.
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top