Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Use C++ language to obtain the local time format, such as12-25-1310: 38: 01.
Use C++ language to obtain the local time format, such as12-25-1310: 38: 01.
Plastic time can be converted into any format you want after it is converted into tm structure. Sample code:

# Contains? & lttime.h & gt

# Contains? & ltcstdio & gt

# Contains? & ltiostream & gt

Use? Namespace? std

/* Time to convert the shaping time into a string format */

int? TimeNumToStr(time_t? Time,? Charles? *pBuf)

{

struct? tm? * tmT

tmT? =? Local time (& time);

What if? (NULL? ==? tmT)

{

//printf("tmT? Is it? Empty. Time? =? %u, \n ",time);

Return? - 1;

}

/* Time format: month-day-year? Hours: minutes: seconds */

sprintf(pBuf,? " %02d-%02d-%02d? %02d:%02d:%02d”,

tmT->; tm_mon? +=? 1,

tmT->; tm_mday,

tmT->; tm_year? -? 100,/* The time in TM is the number of years from 1900 to now, and only two digits are displayed here, so 100*/

tmT->; Tm _ hour,

tmT->; tm_min,

tmT->; Tm _ second

);

Return? 0;

}

int? main(int? argc,? Charles? *argv[])

{

/* Get the current time */

time_t? tmNow? =? Time (empty);

Charles? cTmp[50]? =? {0};

/* The conversion time is in the specified format */

TimeNumToStr(tmNow,cTmp); .

/* Output results */

cout & lt& ltcTmp<。 & ltendl

Return? 0;

} copy the TimeNumToStr function into your code, and pay attention to the related header files. Test results: