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?
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With