如何将日期字符串转换为time_t

How to convert date string to time_t

本文关键字:time 转换 字符串 日期      更新时间:2023-10-16

我试图将" 2018年2月13日星期二"转换为 time_t,我该怎么做,我尝试寻找一个函数而找不到。这是使用未托管的C 。

如果您使用的是C 11,则可以这样做。

#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>
int main()
{
    struct tm tm;
    std::istringstream iss("Tue Feb 13 00:26:36 2018");
    iss >> std::get_time(&tm, "%a %b %d %H:%M:%S %Y");
    time_t time = mktime(&tm);
    std::cout << time << std::endl;
    return 0;
}