c++打卡日期年月日小时格式匹配
时间: 2024-10-06 08:06:20 浏览: 47
在C++中,如果你想要处理日期时间并将其格式化为"年-月-日 时:分:秒"这样的形式,你可以使用 `<chrono>` 和 `<iostream>` 库。下面是一个简单的例子:
```cpp
#include <iostream>
#include <chrono>
#include <ctime>
// 获取当前时间和格式化输出
std::string getCurrentDateTime() {
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
struct tm* local_time = localtime(&t);
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local_time);
return buffer;
}
int main() {
std::string formatted_date_time = getCurrentDateTime();
std::cout << "Current date and time: " << formatted_date_time << std::endl;
// 对于特定的日期格式,可以直接替换"%Y-%m-%d %H:%M:%S"
// 例如,只取年月日可以用 "%Y-%m-%d"
return 0;
}
```
这个程序会获取当前的系统时间,并按照指定的格式输出。如果你想自定义其他格式,只需修改 `strftime` 函数内的格式字符串即可。
阅读全文
相关推荐















