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

Current Date to CString 1

Status
Not open for further replies.

dcusick

Technical User
Aug 9, 2000
271
US
Can anyone help me out and tell me how to get Current Date to a CString variable? All I would need is to take the current date in mm/dd/yyyy format, with slashes and all, and put that into a string. Any help would be greatly appreciated.. Thanks...
 
You can use [tt]CTime::GetCurrentTime()[/tt] to get the current date and time. Then you can use [tt]CTime::Format()[/tt] to format the output into a CString in manner you want!

:)
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Perfect! Thanks gednick, was EXACTLY what I needed... One more quick question for you... Is there any way to add up the Day, Month and Year values and put the result into an integer? For Example, 12/17/2002, would equal 12+17+2002=2031. Thanks again for your help on my first question.

 
Ok, I got it... In case anyone was interested, I just used the GetDay(), GetMonth() and GetYear() functions and assigned them to int values... As easy as that... Thanks again..
 
well that's good but there's also a couple of other methods it would be handy for you to know about.

First, the basic [tt]time_t[/tt] structure, once intialized, will return the number of seconds that have elapsed since 1970 Jan 1st (I think! if I recall correctly).

Therefore, you can get the current time:

[tt]#include <time.h>

time_t theTime;

time(&theTime);
gmtime(&theTime);[/tt]

if you examine the contents of [tt]theTime[/tt] as a numeric value, it will be a very large integer value. In other words, the number of seconds since 1970.

You can therefore, create a time based on one week from now by adding the appropriate number of seconds to [tt]theTime[/tt] - for example, if you want to create some kind of time limited feature in a program.

However, MFC supplies an easier way called [tt]CTimeSpan[/tt] which includes all the necessary operators for examining and comparing time values.

:)
tellis.gif

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

Part and Inventory Search

Sponsor

Back
Top