Re: QDate and two digit year
Quote:
Originally Posted by
Lesiok
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.
Quote:
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.
Re: QDate and two digit year
Quote:
Originally Posted by
wysota
Both QDate constructor and QDate::fromString are parts of QDate. Isn't it ?
Quote:
Originally Posted by
wysota
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".
Re: QDate and two digit year
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.
Re: QDate and two digit year
Quote:
Originally Posted by
SixDegrees
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.
Re: QDate and two digit year
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.
Re: QDate and two digit year
Why you don't just "fix" date. If you now that year 1911 has no sense in your context then:
Code:
QDate date
= QDate::fromString("11-03-23", formatStr
);
if (date.year()<1980 && !formatStr.contains("yyyy")) {
date = date.addYears(100);
}
Re: QDate and two digit year
Quote:
Why you don't just "fix" date.
Or if you simply want the date to always have two-digit year:
Code:
QDate myFromString
( const QString
& date,
const QString
& format
){ d.setDate( d.year()%100, d.month(), d.day() );
return d;
}
Re: QDate and two digit year
Quote:
Originally Posted by
Lesiok
But the remark you mention is in the docs of QDate constructor.
Quote:
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.