Ç - 转换的time_t为字符串格式YYYY-MM-DD HH:MM:SS [英] C - Convert time_t to string with format YYYY-MM-DD HH:MM:SS

查看:196
本文介绍了Ç - 转换的time_t为字符串格式YYYY-MM-DD HH:MM:SS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法来转换一个 time_t的的std ::字符串与格式 YYYY -MM-DD HH:MM:SS 自动同时保持code便携式

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?

推荐答案

使用本地时间来转换 time_t的来一个结构TM 。您可以使用的strftime 从打印所需的数据。

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));

这篇关于Ç - 转换的time_t为字符串格式YYYY-MM-DD HH:MM:SS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆