PDA

View Full Version : QDate and two digit year



Lesiok
23rd March 2011, 19:23
In QDate doc we can read Note that QDate interprets two digit years as is, i.e., years 0 - 99. But this is not true. Yes QDate(11,3,23) constructs object with values year=11, month=3 and day=23. But QDate::fromString("11-03-23","yy-MM-dd") creates object with year=1911.
How can I change this behaviour if possible ?

wysota
23rd March 2011, 20:01
In QDate doc we can read Note that QDate interprets two digit years as is, i.e., years 0 - 99. But this is not true.
Not QDate but the constructor of QDate.

But QDate::fromString("11-03-23","yy-MM-dd") creates object with year=1911.
How can I change this behaviour if possible ?
I guess you can add the two leading zeroes.

Lesiok
24th March 2011, 07:49
Not QDate but the constructor of QDate.
Both QDate constructor and QDate::fromString are parts of QDate. Isn't it ?

I guess you can add the two leading zeroes.
No because this not satisfy format and empty QDate is created. The date string is picked up from widget with system date format which on one computer is defined as "yy-MM-dd".

SixDegrees
24th March 2011, 08:23
You can change the format used by the static function to accept a four-digit year. You can then trivially modify the input string to match that format.

Lesiok
24th March 2011, 09:05
You can change the format used by the static function to accept a four-digit year. You can then trivially modify the input string to match that format.
No I can't. Date format is determined by OS setup.
We are maintaining an application which is using in international corporation.

SixDegrees
24th March 2011, 09:22
QDate::toString() accepts a formatting string; you can set this string to take a four digit year. Your OS may be handing you a two-digit string somewhere else in your program that you're trying to use as input, but it's straightforward to modify that string to be four digits long and QDate::toString() will do what you're asking.

MarekR22
24th March 2011, 09:38
Why you don't just "fix" date. If you now that year 1911 has no sense in your context then:


QString formatStr("yy-MM-dd");
QDate date = QDate::fromString("11-03-23", formatStr);
if (date.year()<1980 && !formatStr.contains("yyyy")) {
date = date.addYears(100);
}

stampede
24th March 2011, 09:42
Why you don't just "fix" date.
Or if you simply want the date to always have two-digit year:

QDate myFromString( const QString& date, const QString& format ){
QDate d = QDate::fromString(date,format);
d.setDate( d.year()%100, d.month(), d.day() );
return d;
}

wysota
24th March 2011, 10:13
Both QDate constructor and QDate::fromString are parts of QDate. Isn't it ?
But the remark you mention is in the docs of QDate constructor.


No because this not satisfy format and empty QDate is created. The date string is picked up from widget with system date format which on one computer is defined as "yy-MM-dd".
And this same format (called ISO-date) specifies how to interpret the date. But this doesn't mean you can't re-interpret it as already suggested. If the application you are maintaining was written in say... 1999 then it is probably safe to assume there are no valid dates before 1999 thus each two digit year has to correspond to the XXI century prefix.