我有下面的代码:
const char* timeformat = "%Y-%m-%d %H:%M:%S";
const int timelength = 20;
char timecstring[timelength];
strftime(timecstring, timelength, timeformat, currentstruct);
cout << "timecstring is: " << timecstring << "\n";
currentstruct 是一个 tm*。 cout 以正确的格式给我日期,但年份不是 2010 年,而是 3910。我知道与从 1900 年开始的年份有关,但我不确定如何让 strftime 识别这个而不是将 1900 添加到那里的 2010 的值,任何人都可以帮忙。
问候
保罗
最佳答案
根据定义,struct tm
的 tm_year
字段是自 1900 年以来的年数。处理 struct tm< 的每一段代码
期望是这种情况。您不能将 tm_year
设置为 2010 年并期望代码神奇地确定您真正指的是 2010 年而不是 3910 年。
将 tm_year
设置为 110,strftime
将起作用。
关于c++ - strftime 不能正确显示年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2578866/