C++ 如何将字符串(2019年10月3日星期四18:45:10)转换为cpp中的时间格式?
将该时间格式存储在其他文件中。并将其作为字符串检索。如何再次将其更改为时间格式。您可以使用C++ 如何将字符串(2019年10月3日星期四18:45:10)转换为cpp中的时间格式?,c++,C++,将该时间格式存储在其他文件中。并将其作为字符串检索。如何再次将其更改为时间格式。您可以使用strtime解析时间,然后mktime将其转换为时间 const char *time_details = "Thu Oct 3 18:45:10 2019"; struct tm tm; // %a or %A // The weekday name according to the current locale, in abbreviated form or the full name. // %b
strtime
解析时间,然后mktime
将其转换为时间
const char *time_details = "Thu Oct 3 18:45:10 2019";
struct tm tm;
// %a or %A
// The weekday name according to the current locale, in abbreviated form or the full name.
// %b or %B or %h
// The month name according to the current locale, in abbreviated form or the full name.
// the field descriptors must match the string format you are providing.
strptime(time_details, "%a %b %d %H:%M:%S %Y", &tm);
time_t t = mktime(&tm);
您可以在找到输入字段描述符的详细用法,我试过了。但是不起作用“不起作用”是什么意思?有错误或输出吗?请分享我的代码:struct tm tim;字符*当前时间;time_t tm=时间(空);当前时间=strtok(ctime(&tm),“\n”);当inputtime(NULL)返回当前时间时,cout输出会因相同的inputtime而变化。您当前的时间正在改变。因此,产出也会发生变化。