3

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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305

3 Answers3

10

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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Dmitry Melnikov
  • 743
  • 4
  • 13
  • Sorry, QT internal documentation does not mention this, but this is usual behaivour of two-digit year interpretators. Qdate::fromString("01", "yy") returns 1901 f.e. [code]QDate fixDate = QDate::fromString(QString("270912"), "ddMMyy").addYears(100);[/code] will solve Your problem – Dmitry Melnikov Sep 27 '12 at 14:21
  • In a way I find this behaviour reasonable. Thank you – Blackbelt Sep 27 '12 at 14:24
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...

cmannett85
  • 21,725
  • 8
  • 76
  • 119
1

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

Look here for more information about fromString method

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Dmitry Stukalov
  • 194
  • 1
  • 13