Important: Please read the Qt Code of Conduct -

QDate::fromString() fails on certain dates

  • Qt 5.15, Qt 6.0, macOS 11.2 Big Sur. These conversions yield invalid dates:

    qDebug() << QDate::fromString("1978-04-02",  "yyyy-MM-dd");
    qDebug() << QDate::fromString("02-04-1978",  "dd-MM-yyyy");code_text

    The following search yields four similar situations (1977-03-27, 1978-04-02, 1979-04-01,

       QString format("yyyy-MM-dd");
       for (int year=1930; year <= 2030; ++year)  {
          QDate date(year,1,1);
          while (date != QDate(year+1,1,1))   {
             QString str = date.toString(format);
             QDate converted = QDate::fromString(str, format);
             if (converted != date)
                qDebug() << date << converted;
             date = date.addDays(1);

    Any ideas?

  • Lifetime Qt Champion

    Create a bug report about this. Works fine on linux.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you post the output you get ?

  • @SGaist the output is

    QDate("1977-03-27") QDate(Invalid)
    QDate("1978-04-02") QDate(Invalid)
    QDate("1979-04-01") QDate(Invalid)
    QDate("1980-03-30") QDate(Invalid)

    Reported as a possible bug, as suggested (QTBUG-91120).

Log in to reply