#include <iostream>
#include <ctime>
int main() {
// 获取当前时间
std::time_t now = std::time(nullptr);
// 将时间转换为本地时间结构体
std::tm* local_time = std::localtime(&now);
// 格式化输出当前时间
char buffer[80];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local_time);
std::cout << "当前时间: " << buffer << std::endl;
return 0;
}
#include <iostream>
和 #include <ctime>
:引入了标准输入输出流和时间处理相关的头文件。std::time_t now = std::time(nullptr);
:使用 std::time
函数获取当前时间,并将其存储在 std::time_t
类型的变量 now
中。**:将时间戳转换为本地时间结构体
std::tm`,方便后续格式化输出。std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local_time);
:使用 std::strftime
函数将时间格式化为字符串,格式为 年-月-日 小时:分钟:秒
。std::cout << "当前时间: " << buffer << std::endl;
:输出格式化后的时间字符串。上一篇:c和c++
下一篇:freopen c++用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站