How to get local time in format yyyyMMddHHmmss?

Hi, could you help me to get local time in string formating like yyyyMMddHHmmss? I know this:

        time_t rawtime;
        struct tm * timeinfo;
        time(&rawtime );
        timeinfo = localtime ( &rawtime );
        printf ( "Current local time and date: %s", asctime (timeinfo) );

But Output:
Current local time and date: Tue Nov 11 15:28:15 2008
Jing272Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

SreejithGCommented:
Using sprintf , you can get the required format. Sample code below
	    time_t rawtime;
        struct tm * timeinfo;
        time(&rawtime );
        timeinfo = localtime ( &rawtime );
        printf ( "Current local time and date: %s", asctime (timeinfo) );
		char timeformat1[50]; // YYYY/MM/DD HH:MM:SS
		sprintf(timeformat1, "%04d/%02d/%2d %02d:%02d:%02d", (1900+timeinfo->tm_year), (timeinfo->tm_mon+1), timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min,timeinfo->tm_sec);
		printf("\nTime in format 1 %s",timeformat1);
 
		char timeformat2[50]; // YYYYMMDDHHMMSS
		sprintf(timeformat2, "%04d%02d%2d%02d%02d%02d", (1900+timeinfo->tm_year), (timeinfo->tm_mon+1), timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min,timeinfo->tm_sec);
		printf("\nTime in format 1 %s",timeformat2);

Open in new window

0

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
Jing272Author Commented:
They are working excellent. Thanks a lot.
0
Infinity08Commented:
Why not use strftime ? It's made for just this purpose :

        http://www.cplusplus.com/reference/clibrary/ctime/strftime.html

sprintf is overly complicated and unnecessary in this case.
char timestamp[64] = "";
strftime(buffer, 64, "Current local time and date: %Y%m%d%H%M%S", timeinfo);

Open in new window

0
Jing272Author Commented:
It is better and simple. Thanks.
0
Infinity08Commented:
Note that the 'buffer' had to be 'timestamp' in my code example obviously :)
0
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
C++

From novice to tech pro — start learning today.