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. Substract two QString values
Forum Updated to NodeBB v4.3 + New Features

Substract two QString values

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstring
7 Posts 3 Posters 2.6k Views 3 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.
  • AlbertoA Offline
    AlbertoA Offline
    Alberto
    wrote on last edited by Alberto
    #1

    Hi,
    I´m trying to substract two hours.
    QString hour_1 = "11:00";
    QString hour_2 = "10:00";

    I would like to save the result from (11:00 - 10:00) into another QString.

    ¿Does anyone know how could I do this?

    Thanks for replying!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you mean

      QString result = hour_1 + " - " + hour_2;
      

      ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • G Offline
        G Offline
        Gruzzyh2
        wrote on last edited by Gruzzyh2
        #3

        Hey,

        I think that you want to compute the difference between the two timestamps ?
        Take a look at QTime (http://doc.qt.io/qt-5/qtime.html) and don't use QString. I think you should try to use a suitable type for your var.
        Two solutions :

        • Either you get a time from QString so you can try something like this :

        // Time in QString
        QString sHour_1 = "11:00";
        QString sHour_2 = "10:00";

        // Time in QTime from QString
        QTime hour_1 = QTime::fromString(sHour_1, "hh:mm");
        QTime hour_2 = QTime::fromString(sHour_2, "hh:mm");

        // hour_2 in seconds to substracts from hour_1
        int secs = hour_2.second();

        QTime result = hour_1.addSecs(-secs); // minus 'cause secs is positive

        result.toString("hh:mm"); // will get you a QString "01:00"

        • Or you could have taken the times in QTime from the beginning :

        QTime hour_1 (11, 0, 0);
        QTime hour_2 (10, 0, 0);

        Then you do the same as above.

        Gruz

        1 Reply Last reply
        0
        • AlbertoA Offline
          AlbertoA Offline
          Alberto
          wrote on last edited by
          #4

          I did this:

          QTime hour_1 = QTime::fromString(sHour_1,"hh:mm");
          QTime hour_2 = QTime::fromString(sHour_2,"hh:mm");
          
          qDebug()<<hour_2;
          
          int secs = hour_2.second();
          
          qDebug()<<secs;
          
          QTime result = hour_1.addSecs(-secs);
          QString result2 =result.toString("hh:mm");
          
          qDebug()<<result;
          qDebug()<<result2;
          
          

          But the result obtained is this:
          "11:00"
          "10:00"
          QTime("10:00:00.000")
          0
          QTime("11:00:00.000")
          "11:00"

          Sorry for my English.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gruzzyh2
            wrote on last edited by
            #5

            Can you match every qDebug with the line please ?
            (np for the english mine is bad too)

            AlbertoA 1 Reply Last reply
            0
            • G Gruzzyh2

              Can you match every qDebug with the line please ?
              (np for the english mine is bad too)

              AlbertoA Offline
              AlbertoA Offline
              Alberto
              wrote on last edited by
              #6

              @Gruzzyh2

              qDebug()<<sHour_1;
              "11:00"

              qDebug()<<sHour_2;
              "10:00"

              qDebug()<<hour_2;
              QTime("10:00:00.000")

              qDebug()<<secs;
              0

              qDebug()<<result;
              QTime("11:00:00.000")

              qDebug()<<result2;
              "11:00"

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                A more simpler solution:

                    QTime time1 = QTime::fromString("11:00", "HH:mm");
                    QTime time2 = QTime::fromString("10:00", "HH:mm");
                
                    qDebug() << QTime::fromMSecsSinceStartOfDay(time2.msecsTo(time1));
                

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

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