c++获取当前时间
2026-01-18 11:13:46
发布于:江苏
1阅读
0回复
0点赞
#include <iostream>
#include <chrono>
#include <iomanip>
#include <sstream>
stdstring getCurrentTime() {
// 获取当前时间点
auto now = stdchronosystem_clocknow();
// 转换为time_t类型
auto now_c = stdchronosystem_clockto_time_t(now);
// 使用localtime获取本地时间
stdtm* now_tm = std::localtime(&now_c);
// 使用stringstream将时间转换为字符串
std::stringstream ss;
ss << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S");
return ss.str();
}
int main() {
stdstring timeStr = getCurrentTime();
stdcout << "Current time is: " << timeStr << std::endl;
return 0;
}
这里空空如也


有帮助,赞一个