Results 1 to 9 of 9

Thread: QDate and two digit year

  1. #1
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDate and two digit year

    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 ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDate and two digit year

    Quote Originally Posted by Lesiok View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDate and two digit year

    Quote Originally Posted by wysota View Post
    Not QDate but the constructor of QDate.
    Both QDate constructor and QDate::fromString are parts of QDate. Isn't it ?
    Quote Originally Posted by wysota View Post
    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".

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDate and two digit year

    Quote Originally Posted by SixDegrees View Post
    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.

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  7. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:
    Qt Code:
    1. QString formatStr("yy-MM-dd");
    2. QDate date = QDate::fromString("11-03-23", formatStr);
    3. if (date.year()<1980 && !formatStr.contains("yyyy")) {
    4. date = date.addYears(100);
    5. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDate and two digit year

    Why you don't just "fix" date.
    Or if you simply want the date to always have two-digit year:
    Qt Code:
    1. QDate myFromString( const QString& date, const QString& format ){
    2. QDate d = QDate::fromString(date,format);
    3. d.setDate( d.year()%100, d.month(), d.day() );
    4. return d;
    5. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDate and two digit year

    Quote Originally Posted by Lesiok View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Happy New Year!!!
    By fengtian.we in forum General Discussion
    Replies: 6
    Last Post: 4th January 2012, 06:07
  2. QDate SystemLocaleShortDate without year
    By martinn in forum Qt Programming
    Replies: 3
    Last Post: 24th February 2010, 18:17
  3. hello. happy new year
    By Andrewliu in forum General Discussion
    Replies: 4
    Last Post: 30th December 2006, 19:35
  4. QDateEdit and 2 digit years
    By Byngl in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2006, 23:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.