Important: Please read the Qt Code of Conduct -

QDate conversions from/to string inconsistent for QLocale::ShortFormat (issue with the century)

  • Looks like a wrong feature in Qt (to avoid to call it a bug).


       QDate qdate(2020,12,05);
       QString qstring = QLocale::system().toString(qdate, QLocale::ShortFormat);
       QDate qdate2 = QDate::fromString(qstring, QLocale::system().dateFormat(QLocale::ShortFormat));
       QString qstring2 = QLocale::system().toString(qdate, QLocale::ShortFormat);
        #define __DV(x) qDebug() << #x " =" << x


    qdate = QDate("2020-12-05")
    qstring = "05-12-20"
    qdate2 = QDate("1920-12-05")
    qstring2 = "05-12-20"

    So, in the above code, I do not encode date strings myself, I'm just letting Qt to generate strings using QLocale::ShortFormat and interpreting its own result back to QDate, still using QLocale::ShortFormat. And Qt is mishandling the century.

    Please note that I only need to work in the 21st century here. That's Qt that transform it in 20th century's date

    The long format is too much for me but mm-dd-yyyy would be OK. I need to keep using QLocale. I need to save the date in a text file(i.e. as a string), and later read it back. Also I need to show it the shortest possible way in a QTableWidget.

    The problem is that QDate string formatting in connection to QLocale is not very flexible, and Qt doesn't have a consistent interpretation in both direction of the QDate to/from QString conversions.

    Of course, I may for sure solve this the long way (in terms of coding and runtime), but I wonder if there something that I'm doing wrong and that could be solved easily and a more clean way?

  • Lifetime Qt Champion

    The problem comes from - can't change it for Qt5 anymore since it may break existing implementations but maybe it's not too late for Qt6.

Log in to reply