Click here to Skip to main content
Click here to Skip to main content

Date-Time Conversion Utility

By , 21 Sep 2001
 

Sample Image

Introduction

Lately I've had to do a lot of work with CTime and COleDateTime object variables. They're pretty nice classes to work with but not when you're debugging and need to know their actual date-time values. The CTime class stores its value in a time_t member variable and COleDateTime uses the DATE type to keep its value. Unfortunately when we need to know what they mean in human-readable format, such as 11/03/1999 14:08:56, it's not possible without explicitly writing some code to format them into a string. Well, I got tired of writing such code so instead I wrote a little application where I could input the value I need to figure out and then have it converted at the click of a button. The application is called DTConverter and its a simple MFC dialog-based application that allows you to convert to and from a time_t, a DATE, or a regular string in %m/%d/%Y %H:%M:%S format.

I hope it makes debugging your date-time objects less painful. Enjoy it!

Update by Geert Delmeiren

I use COleDateTimeSpan variables. These are also non-human readable, so I extended the utility for converting COleDateTimeSpans also.

For converting a variable a couple of mouse clicks are needed:

  1. Ctrl-C to copy the variable's value
  2. Click on the utility's session in the task bar
  3. Click in the edit field you want to paste
  4. Ctrl-V to paste the value
  5. Click on the button to convert the value

That could be quicker! So I extended the utility with (configurable) auto-paste/auto-convert functionality. In the fully automatic mode steps 3) to 5) are obsolete. You just copy the value and activate the utility and... there's the result!

Another small change: The human readable date/time string now uses the format specified in the regional settings.

History

22 Sep 2001 - added 'Always on Top' and disabling GUI logic to the dialogs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Alvaro Mendez
Web Developer
United States United States
Member
I've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to find 3rd friday date in every month in every yearmembertns_ranjith26 Mar '08 - 19:24 
Hi...
How to find 3rd friday date in every month in every year...help me
Generalits good stuffmembersri sankaradoss20 Jun '07 - 9:19 
Hi.. really its good tool.. its very useful for me.. thanks buddy..
 
Sankaradoss Srirengan, Verizon Data Services Inc., Tampa, FL 33637.
GeneralNice demomembertommyplus225 Oct '06 - 17:51 
Thanks for the demo Smile | :)
GeneralConvert CString to CTime / COleDateTimememberP-Rex14 Jun '05 - 4:36 
Hi guys,
 
I have a string, for example '1085462900'. I have to convert this string into a CTime / COleDateTime value. How can i do this?
I get this string from a oracle database.
 
P. Cool | :cool:
GeneralRe: Convert CString to CTime / COleDateTimememberAlvaro Mendez15 Jun '05 - 14:35 
If you know the format the string is in, I believe you can use the Parse function of those classes.
 
Regards,
Alvaro
 

Victory means exit strategy, and it's important for the President to explain to us what the exit strategy is. -- GWB, 1999.
GeneralRe: Convert CString to CTime / COleDateTimesussWietsie28 Jun '05 - 7:59 
#include
#include
 
#include
#include
#include
 
void main()
{
char tmpbuf[128];
time_t stime;
 
struct tm * today ;
time( &stime );
 
today = localtime( &stime );
//today = localtime( 1119856245 ); I thought this
 
printf( "stime = %d \n", stime);
printf( "today = %d \n", today);
 
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y %.\n", today );
}
 
Just a start, wel it also doesnt work.

GeneralRe: Convert CString to CTime / COleDateTimesussAnonymous28 Jun '05 - 7:52 
Nothing to find on internet for this, who can help?

QuestionRe: Convert CString to CTime / COleDateTimememberDavidCrow4 Aug '06 - 4:23 
P-Rex wrote:
I have a string, for example '1085462900'.

 
If this is supposed to be a date/time value, what format is it in? Without knowing at least that much, it's going to be hard to assign it to a CTime/COleDateTime object.
 

"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

"Judge not by the eye but by the heart." - Native American Proverb


GeneralyGood job!sussAnonymous10 Mar '05 - 10:03 
Thanks, helped my debugging a lot. Big Grin | :-D
QuestionCDate function in C#???memberQamarwis19 Jan '04 - 9:19 
I have a web form in which i take user'sdate of birth.now i take the day, month n year seperately. i then concatenate it into a single string. now my problem is that how can i convert it nto date type?? like is there any function in C# like cdate in VB.net???
 
Waiting 4 reply....
AnswerRe: CDate function in C#???memberAlvaro Mendez19 Jan '04 - 10:06 
Look at System.DateTime.Parse.
 
Regards,
Alvaro
 

 

"I do" is both the shortest and the longest sentence in the English language.
GeneralDATE problemsussanirban maiti4 Jun '03 - 20:55 
i have created a COM component in VB which has DATE data type in one of interface function. when i call the function from VC.. its booming..
0.0000000000 value is returned..
can anybody explain me the problem.. and what can be probable solution to this..
anirban
GeneralCString to DateTimememberKomtiki11 Apr '02 - 7:38 
Does anybody know of a function that can correctly convert a CString to a Date or a time value.
 
I'm busy with a T&A and Payroll System and the people that wrote the access control wrote everything in to their database as text.Cry | :((
 
So PLEASE can anybody help.
GeneralRe: CString to DateTimememberKarstenK29 Apr '02 - 3:09 
Try "COleDateTime::ParseDateTime" from MFC or you got to parse the string to get the substrings of the values (hh:mm:ss dd/mm/yyyy) and retrieve the numbers via "atol" function and then create a CTime. It is depending on your format.
 
I hope it helps Smile | :)
GeneralRe: CString to DateTimememberKomtiki29 Apr '02 - 18:27 
Thanx problem sorted some time ago using ParseDateTime.
 
I have a problem with Datetime calculations though. I writing a costing application and I need to know how many hours where worked in a month. Problem is COleDateTime only calculates up to 24 hours and I need more. COleDateTimeSpan on the the other hand returns ex. 1 day and 16:45 hours which means I have to process that result back to a number and then get a result.
 
Do you know of an easier way?
GeneralRe: CString to DateTimesussVandana G25 Jul '02 - 13:16 
CString x ="1/1/2001 10:12:12";
 
int m, d, Y, H, M, S;
sscanf("%d/%d/%d %d:%d:%d", m, d, Y, H, M, S);
 
CTime t(Y, m, d, H, M, S);
GeneralRe: CString to DateTimesussVandana G25 Jul '02 - 13:17 
CString x ="1/1/2001 10:12:12";
 
int m, d, Y, H, M, S;
sscanf(x, "%d/%d/%d %d:%d:%d", &m, &d, &Y, &H, &M, &S);
 
CTime t(Y, m, d, H, M, S);

GeneralRe: CString to DateTimememberGordon D.Marcus15 Apr '03 - 20:46 
Just if anyone knows...
 
How to convert formatted string into CTime\COleDateTime for the cases like this:
 
CString str = "Monday, 01-Jan-2001";
???
 
Statement:
 
COleDateTime tm;
tm.ParseDateTime(str);

 
doesn't work for me Cry | :((

GeneralAlternative methodmemberKeith Bussell24 Sep '01 - 16:33 
Nice work. I used to deal with this problem by writing small conversion functions and then running them in the watch window. The one advantage to doing it that way is the string representation automatically updates while stepping through the app.
 
     -keithb
GeneralRe: Alternative methodmemberMarcello4 Mar '02 - 19:26 
Good !
 
But I am lazy...
 
... can you post here your code, so we don't have to replicate the work you did ?
 
Thank you,
Marcello
GeneralRe: Alternative methodmembermwilliamson4 Aug '03 - 16:36 
An even better method is to create an auto expression that calls COleDateTime::Format. Then you don't need to do anything else.
GeneralCOleDateTimeSpan flawmemberMike Osbahr14 Aug '01 - 4:23 
It should be noted when using COleDateTimeSpan that this class does not account for daylight savings time.
 
Where DST is used it may be necessary to convert COleDateTime values to CTime values before calculating time spans.
 
>>>-----> MikeO

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 22 Sep 2001
Article Copyright 1999 by Alvaro Mendez
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid