Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Substract two QString values

    General and Desktop
    qstring
    3
    7
    2256
    Loading More Posts
    • 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.
    • Alberto
      Alberto last edited by Alberto

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 1
        • G
          Gruzzyh2 last edited by Gruzzyh2

          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 Reply Quote 0
          • Alberto
            Alberto last edited by

            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 Reply Quote 0
            • G
              Gruzzyh2 last edited by

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

              Alberto 1 Reply Last reply Reply Quote 0
              • Alberto
                Alberto @Gruzzyh2 last edited by

                @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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 1
                  • First post
                    Last post