C++之获取格式化当前时间
本文最后更新于 2024-12-09,文章内容可能已经过时。
一、C++格式化时间及获取当前时间
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
string getTime() {
const time_t now = time(nullptr);
char str[64];
strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", localtime(&now));
return str;
}
int main(int argc, char *argv[]) {
//0.打印当前时间
const string timeStr = getTime();
cout << timeStr << endl;
return 0;
}
2024-12-09 17:32:30
Process finished with exit code 0
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 软件从业者Hort
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果