获取当前时间并格式化

功能和代码都很简单,直接上代码

string getFormatTime(const char* format, const int len)
{
    time_t curtm = time(nullptr);
    struct tm* tm_ptr;
    tm_ptr = localtime(&curtm);
    char *chtm = new char[len+1];
    strftime(chtm, len + 1, format, tm_ptr);
    string str(chtm);
    delete[] chtm;
    return str;
}

示例如下:

getFormatTime("%Y-%m-%d %H:%M:%S", 19);

返回string是个人习惯,毕竟一直做c++开发

原文地址:https://www.cnblogs.com/superbi/p/4977153.html