Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Convert time_t to string with format YYYY-MM-DD HH:MM:SS

Tags:

c

Is there any way to convert a time_t to a std::string with the format YYYY-MM-DD HH:MM:SS automatically while keeping the code portable?

like image 968
dmessf Avatar asked Jun 16 '10 14:06

dmessf


1 Answers

Use localtime to convert the time_t to a struct tm. You can use strftime to print the desired data from that.

char buff[20]; time_t now = time(NULL); strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&now)); 
like image 126
Jerry Coffin Avatar answered Sep 26 '22 21:09

Jerry Coffin