【发布时间】:2023-04-07 13:33:02
【问题描述】:
我通过显示当前日期/时间
#include <ctime>
time_t sec = time(NULL);
tm* curTime = localtime(&sec);
cout << "Date: " << curTime->tm_mday << "." << curTime->tm_mon << "." << curTime->tm_year+1900 << endl;
cout << "Time: " << curTime->tm_hour << ":" << curTime->tm_min << ":" << curTime->tm_sec << endl;
实际上它显示例如
Date: 4.10.2016
Time: 9:54:0
我在这里遇到了 2 个问题:
- 我想要两位数字,日期(日和月)和时间(小时、分钟和秒)。所以它应该显示 04.10.2016 和 09:54:00
- 今天显示的是 24.10.2016,但今天是 24.11.2016。为什么显示的是 10 月而不是 11 月? Linux 时钟正确显示时间。
感谢您的帮助:)
【问题讨论】:
-
2.月份的索引从 0 到 11(0 是 1 月,11 是 12 月)所以你必须 +1 到数字
-
您使用哪种 C++ 标准?
-
我正在使用 -std=c++0x,因为 -std=c++11 在我的计算机上没有以某种方式激活(而且我无权更改它)。
标签: c++ date time digits ctime