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. Date Comparison
Forum Updated to NodeBB v4.3 + New Features

Date Comparison

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.7k 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.
  • Scott KriseS Offline
    Scott KriseS Offline
    Scott Krise
    wrote on last edited by
    #1

    Im simply trying to compare two dates...and its not working properly. Here is what im trying, but no luck. Ive tried a number of different ways, but its still failing. Regardless of which date is earlier, it always hits my "do this" logic. Im assuming the code below is failing because of the string? But Ive tried comparing them as dates too...but no luck.

    What am I missing?

    QString myTargetDate = QDate::currentDate().addDays(-3).toString("yyyy-MM-dd");
    QString myShipDate = ui->shipDateEdit->date().toString("yyyy-MM-dd");

    if (myShipDate < myTargetDate);
    {
    do this
    }

    VRoninV Pablo J. RoginaP 2 Replies Last reply
    0
    • Scott KriseS Scott Krise

      Im simply trying to compare two dates...and its not working properly. Here is what im trying, but no luck. Ive tried a number of different ways, but its still failing. Regardless of which date is earlier, it always hits my "do this" logic. Im assuming the code below is failing because of the string? But Ive tried comparing them as dates too...but no luck.

      What am I missing?

      QString myTargetDate = QDate::currentDate().addDays(-3).toString("yyyy-MM-dd");
      QString myShipDate = ui->shipDateEdit->date().toString("yyyy-MM-dd");

      if (myShipDate < myTargetDate);
      {
      do this
      }

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      @Scott-Krise said in Date Comparison:

      Im assuming the code below is failing because of the string?

      Not because of it but you should really, really keep it a QDate as it's infinitely more efficient

      it always hits my "do this" logic

      As it should. The block does not depend on the if

      To solve remove the ; at the end of if (myShipDate < myTargetDate);

      Going forward increasing the level of compiler warnings should be enough to spot these kind of problems

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • Scott KriseS Scott Krise

        Im simply trying to compare two dates...and its not working properly. Here is what im trying, but no luck. Ive tried a number of different ways, but its still failing. Regardless of which date is earlier, it always hits my "do this" logic. Im assuming the code below is failing because of the string? But Ive tried comparing them as dates too...but no luck.

        What am I missing?

        QString myTargetDate = QDate::currentDate().addDays(-3).toString("yyyy-MM-dd");
        QString myShipDate = ui->shipDateEdit->date().toString("yyyy-MM-dd");

        if (myShipDate < myTargetDate);
        {
        do this
        }

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @Scott-Krise why are converting dates to string? Why not to use the less than operator directly? See here for an example...

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • Pradeep P NP Offline
          Pradeep P NP Offline
          Pradeep P N
          wrote on last edited by
          #4

          Hi @Scott-Krise

          As said by @Pablo-J-Rogina use the QDate directly. Below is the sample code.

           QDate date1, date2;
          
              auto result1 = (date1.currentDate() == date2.currentDate());
          
              date1 = date1.currentDate().addDays(5);
          
              auto result2 = (date1 > date2.currentDate());
          
              qDebug() << "result1 : " << result1
                       << "\nresult2 : " << result2
                       << endl;
          
          

          Output:

          result1 :  true 
          result2 :  true 
          

          All the best.

          Pradeep Nimbalkar.
          Upvote the answer(s) that helped you to solve the issue...
          Keep code clean.

          Scott KriseS 1 Reply Last reply
          2
          • Pradeep P NP Pradeep P N

            Hi @Scott-Krise

            As said by @Pablo-J-Rogina use the QDate directly. Below is the sample code.

             QDate date1, date2;
            
                auto result1 = (date1.currentDate() == date2.currentDate());
            
                date1 = date1.currentDate().addDays(5);
            
                auto result2 = (date1 > date2.currentDate());
            
                qDebug() << "result1 : " << result1
                         << "\nresult2 : " << result2
                         << endl;
            
            

            Output:

            result1 :  true 
            result2 :  true 
            

            All the best.

            Scott KriseS Offline
            Scott KriseS Offline
            Scott Krise
            wrote on last edited by
            #5

            OMG. A mis-placed semicolon. How embarassing! Ya, it makes perfect sense why it was doing what it was now that I noticed that! Thanks everyone.

            Paradeep, to answer your question, I started with QDate...then when that didn't work I started trying things based on internet searches and led me to trying the string option.

            The interesting when you google how to compare dates in C++ how many abstract solutions you find. It really shouldn't be that difficult!! Thanks again!

            Pablo J. RoginaP 1 Reply Last reply
            0
            • Scott KriseS Scott Krise

              OMG. A mis-placed semicolon. How embarassing! Ya, it makes perfect sense why it was doing what it was now that I noticed that! Thanks everyone.

              Paradeep, to answer your question, I started with QDate...then when that didn't work I started trying things based on internet searches and led me to trying the string option.

              The interesting when you google how to compare dates in C++ how many abstract solutions you find. It really shouldn't be that difficult!! Thanks again!

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @Scott-Krise said in Date Comparison:

              how to compare dates in C++

              Well, that's the magic of Qt framework: you have a level of abstraction that also provide multi-platform solutions...

              PS: please don't forget to mark your post as solved!

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              Scott KriseS 1 Reply Last reply
              0
              • Pablo J. RoginaP Pablo J. Rogina

                @Scott-Krise said in Date Comparison:

                how to compare dates in C++

                Well, that's the magic of Qt framework: you have a level of abstraction that also provide multi-platform solutions...

                PS: please don't forget to mark your post as solved!

                Scott KriseS Offline
                Scott KriseS Offline
                Scott Krise
                wrote on last edited by
                #7

                One other question. Id like to take vronin's suggestion and increase my warning level...but not sure where to do that....or what warning level you suggest?

                Thanks again.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  Try adding to your .pro file:

                  QMAKE_CFLAGS_WARN_ON -= -W3
                  QMAKE_CFLAGS_WARN_ON += -W4
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  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