Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDate comparison
Qt 6.11 is out! See what's new in the release blog

QDate comparison

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 8.8k Views 2 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.
  • W Offline
    W Offline
    WhatIf
    wrote on last edited by WhatIf
    #1

    Hi,

    I'm trying to compare dates. One provide by user, QDateEdit, against what's in the database. I searched online and found daysTo(), but there is one issue using this function.

    qint64 QDate::daysTo(const QDate &d) const
    Returns the number of days from this date to d (which is negative if d is earlier than this date).
    Returns 0 if either date is invalid.

    it returns 0 if either date is invalid, which I didn't run into yet but if the dates are the same, it also returns 0. Is there any other function to use to bypass this?

    K Ni.SumiN 3 Replies Last reply
    0
    • W WhatIf

      Hi,

      I'm trying to compare dates. One provide by user, QDateEdit, against what's in the database. I searched online and found daysTo(), but there is one issue using this function.

      qint64 QDate::daysTo(const QDate &d) const
      Returns the number of days from this date to d (which is negative if d is earlier than this date).
      Returns 0 if either date is invalid.

      it returns 0 if either date is invalid, which I didn't run into yet but if the dates are the same, it also returns 0. Is there any other function to use to bypass this?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @WhatIf

      Did you see the relational operators are provided for QDate?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • W WhatIf

        Hi,

        I'm trying to compare dates. One provide by user, QDateEdit, against what's in the database. I searched online and found daysTo(), but there is one issue using this function.

        qint64 QDate::daysTo(const QDate &d) const
        Returns the number of days from this date to d (which is negative if d is earlier than this date).
        Returns 0 if either date is invalid.

        it returns 0 if either date is invalid, which I didn't run into yet but if the dates are the same, it also returns 0. Is there any other function to use to bypass this?

        Ni.SumiN Offline
        Ni.SumiN Offline
        Ni.Sumi
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • W WhatIf

          Hi,

          I'm trying to compare dates. One provide by user, QDateEdit, against what's in the database. I searched online and found daysTo(), but there is one issue using this function.

          qint64 QDate::daysTo(const QDate &d) const
          Returns the number of days from this date to d (which is negative if d is earlier than this date).
          Returns 0 if either date is invalid.

          it returns 0 if either date is invalid, which I didn't run into yet but if the dates are the same, it also returns 0. Is there any other function to use to bypass this?

          Ni.SumiN Offline
          Ni.SumiN Offline
          Ni.Sumi
          wrote on last edited by
          #4

          @WhatIf

          Both comparing dates MUST in the same format. check here. Use fromString .
          If both dates are same then it gives the 0. If you want even to calculate the hours, minutes and seconds if the date is same then you have to use QDateTime along with some mathematical calculation.

          QDateTime currentTime = QDateTime::currentDateTime();
          QDateTime userTime = QDateTime::fromString(timefromuser, "yyyy-M-d h:m:s"); //both must be in same format.
          long seconds = currentTime.secsTo(userTime);
          int secondss = (int)(seconds) % 60;
          int minutes = (int)((seconds / (60)) % 60);
          int hours = (int)((seconds / (60 * 60)) % 24);
          int days = (int)((seconds) / (60 * 60 * 24));
          1 Reply Last reply
          0
          • Paul ColbyP Offline
            Paul ColbyP Offline
            Paul Colby
            wrote on last edited by
            #5

            @WhatIf said:

            it returns 0 if either date is invalid, which I didn't run into yet but if the dates are the same, it also returns 0.

            To different between those two scenarios, I would first check that the dates are valid, like:

            if ((!date1.isValid()) || (!date2.isValid())) {
                // do some error handling.
            } else {
                const qint64 days = date1.daysTo(date2);
            }
            

            Cheers.

            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 Extensions
            • Unsolved