Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt4.8.1 QDate::fromString issue with negative years
Qt 6.12 LTS is out! See what's new in the release blog

Qt4.8.1 QDate::fromString issue with negative years

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Razamut
    wrote on last edited by
    #1

    Using Qt4.8.1, not tried in Qt5

    Here is small example:
    @
    QDate test = QDate::fromString("-0005.01.01", "yyyy.MM.dd");
    if(test.isValid()){
    PfGlobal::debug_other_debug("PfGlobal::StringToDate", tr("Valid date from QString"));
    }else{
    PfGlobal::debug_other_debug("PfGlobal::StringToDate", tr("Invalid date from QString")); // always returned
    }
    test = QDate(-5, 1, 1); // same date created directly from int numbers
    if(test.isValid()){
    PfGlobal::debug_other_debug("PfGlobal::StringToDate", tr("Valid date from QDate")); // returned
    }else{
    PfGlobal::debug_other_debug("PfGlobal::StringToDate", tr("Invalid date from QDate"));
    }
    @

    I have tried with other dates like "-5.01.01", "-1000.01.01", but QDate::fromString always return invalid date.

    In the function help it is written: "yyyy The year as four digit number. If the year is negative, a minus sign is prepended in addition.", so I understood it that way:

    @
    QDate test = QDate::fromString("-0005.01.01", "-yyyy.MM.dd"); // return QDate January 1st 5AD instead of BC
    test.setYMD(-test.year(), test.month(), test.day()); // fix year to be negative
    @

    Is there a way to use QDate::fromString which automatically interpret the date as BC/AD ? Or is it necessary to use a workaround as above when both positive and negative dates are possible?

    Thanks,
    Razamut

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bowdzone
      wrote on last edited by
      #2

      Facing the exact same issue right now in 5.2.1. Either I'm overseeing something simple or this is a bug...

      This is my (somewhat ugly) workaround to cope with both negative and positive dates
      @
      std::string str = "01.01.-1000";
      int year_negative = str.find('-');
      if(year_negative != std::string::npos){
      str.erase(std::remove(str.begin(), str.end(), '-'), str.end());
      }
      QDate tmp_date = QDate::fromString(QString::fromStdString(str), "dd.MM.yyyy");
      tmp_date.setDate((year_negative != std::string::npos) ? (-1)*tmp_date.year() : tmp_date.year(), tmp_date.month(), tmp_date.day());
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved