C++ strftime() 不正确的输出

标签 c++ datetime strftime

#include <ctime>
#include <iostream>

using namespace std;

int main()
{
    char dateStringBuffer[256] = "";
    tm date;

    date.tm_year = 2017;
    date.tm_mon = 9;
    date.tm_mday = 13;

    strftime(dateStringBuffer, 256, "%d.%m.%Y", &date);
    cout << dateStringBuffer;
}

输出是:

13.10.3917

预期输出:

13.09.2017

这对我来说毫无意义。我做错了什么?

最佳答案

corecrt_wtime.h 中的 struct tm 在其成员旁边有以下注释。

int tm_mon;   // months since January - [0, 11]
int tm_year;  // years since 1900

这意味着你应该做这样的事情:

date.tm_year = 117;
date.tm_mon = 8;
date.tm_mday = 13;

这将为您提供预期的输出:13.09.2017

关于C++ strftime() 不正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49647524/

相关文章:

java - 使用 JODA 进行日期格式化 - AM 格式错误

c# - 有什么方法可以更改 JSON 中的日期时间格式吗?

c++ - boost local_date_time 数学错了吗?

python - 将 30 分钟添加到从文本文件读取的时间并与当前时间进行比较

sqlite - SQLite 中的 strftime 函数不起作用

c++ - 为什么这个 pop() 函数会产生段错误?

c++ - 无法从模板类内的模板函数类调​​用类函数

android - 在 android 中使用 sqlite 的 strftime 函数

c++ - 从CreateProcess调用c中的msi文件

c++ - 如何知道 QStyle 中的标题文本宽度?