Cpp获取当前时间字符串
程序员文章站
2022-07-14 19:46:06
...
#include <iostream>
std::string GetNowTime() {
time_t setTime;
time(&setTime);
tm* ptm = localtime(&setTime);
std::string time = std::to_string(ptm->tm_year + 1900)
+ "/"
+ std::to_string(ptm->tm_mon + 1)
+ "/"
+ std::to_string(ptm->tm_mday)
+ " "
+ std::to_string(ptm->tm_hour) + ":"
+ std::to_string(ptm->tm_min) + ":"
+ std::to_string(ptm->tm_sec);
return time;
}
//test
int main(int argc, char *argv[]) {
std::string strTime = GetNowTime();
std::cout << "now time: " << strTime << std::endl;
return 0;
}
上一篇: R语言线性回归
下一篇: 获取指定长度的随机字符串