我通过显示当前日期/时间
#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。为什么它显示十月而不是十一月? Linux 时钟正确显示时间。
感谢您的帮助:)
最佳答案
您应该使用操纵器进行打印。 在 printf("%02d", curTime->tm_hour) 在 cout 中,你可以使用, std::cout << std::setw(2) << std::setfill('0') << curTime->tm_hour.
tm_mon 是从 0 到 11。所以你应该使用 tm_mon+1 来打印。
关于C++ 当前时间 -> 两位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40781934/