QDate - wrong year

落爺英雄遲暮 提交于 2019-12-02 03:18:37

问题


I have the following situation:

   QDate fixDate = QDate::fromString(QString("270912"), "ddMMyy");

the year returned is 1912. I do not understand why and how get the correct year.

Thanks in advance


回答1:


Two-digit year is always interpretating as 19xx. So You can pass YYYY or just add 100 to years.




回答2:


As described in the docs:

For any field that is not represented in the format the following defaults are used:
Year 1900
Month 1
Day 1

// For example:
QDate::fromString("1.30", "M.d");           // January 30 1900
QDate::fromString("20000110", "yyyyMMdd");  // January 10, 2000

(Excuse the formatting, it's in a table in the docs). So you are going to have to pass the full year into the method until Qt decide 2012 is far enough into the century to change the default...




回答3:


Could you use ddMMyyyy instead of ddMMyy? Or you need date in this format?

Look here for more information about fromString method



来源:https://stackoverflow.com/questions/12622654/qdate-wrong-year

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!