Time as a null-terminated string.

How do I get the local time in the format HH:MM:SS into a null-terminated string using MS Visual C++ 4?  I know how to use GetLocalTime() to fill up the structure, but how do I convert the hours, minutes and seconds into a simple null-terminated string separated by colons and append the terminating zero?  Could you please post some C++ source code which will do this.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you insist in using 'GetLocalTime()', the solution is

char acTime [ 256];

GetLocalTime ( &st);


Or you could use

BOOL GetTimeStamp( char *pcBuffer, unsigned short usBufSize)
 time_t         Sec70;
 struct tm     *pActTime;

 char           acTimeStamp[MAX_TIMESTAMP +1];

    if ( MAX_TIMESTAMP +1 < usBufSize ) return(FALSE);

    time( &Sec70); /* Get time in seconds since 0:00am 1.1.1970 */

    pActTime = localtime( &Sec70); /* put it in a more reasonable format */

    sprintf( acTimeStamp,       /* arrange the interesting values in a string */
             pActTime->tm_mon +1,

    strncpy( pcBuffer, acTimeStamp, MAX_TIMESTAMP +1);


Feel free to ask if you need more information!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Oops, I almost forgot: You could of course also use '_strtime()'
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.