Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calculate elapsed time inside QML

Calculate elapsed time inside QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 1.1k 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by Damian7546
    #1

    Hi,

    I need calculate elapsed time form readed timestamp.
    In QML I do like below:

    var readed = new Date(timestamReaded * 1000)
    var actual = new Date()
    var elapsed = new Date(actual - readed)
    
    function roundToHour(date){
    	var p = 60*60*1000;
    	return new Date(Math.round(date.getTime() /p)*p)
    }
    
    var elapsedAround = roundToHour(elapsed);
    
    mytextfield.text = elapsedAround.getHours()
    

    It works, but only when reded timestamp and actual date is in the same day.... What I should change to calculate hours when readed timestamp is from previus day ?

    piervalliP 1 Reply Last reply
    0
    • D Damian7546

      @piervalli I am still don't understand ....

      piervalliP Offline
      piervalliP Offline
      piervalli
      wrote on last edited by piervalli
      #5

      @Damian7546
      If you need to calculate diff date, you can use a simple js algoritme.
      You can use a simple js date

      function inDays (d1, d2) {
                  var t2 = d2.getTime();
                  var t1 = d1.getTime();
      
                  return Math.floor((t2-t1)/(24*3600*1000));
              }
       function inHours (d1, d2) {
                  var t2 = d2.getTime();
                  var t1 = d1.getTime();
      
                  return Math.floor((t2-t1)/(3600*1000));
              }
      
      Component.onCompleted:{
              var d1 = new Date()
              var d2 = new Date(2023,3,1)
              console.error("inDays",inDays(d1,d2))
          }
      

      Olther examples are in the link of stackoverflow, for hours

      Instanded console.time and console.timeEnd is used to calculated diff time in a function (benchmark)

      https://stackoverflow.com/questions/7763327/how-to-calculate-date-difference-in-javascript

      1 Reply Last reply
      0
      • D Damian7546

        Hi,

        I need calculate elapsed time form readed timestamp.
        In QML I do like below:

        var readed = new Date(timestamReaded * 1000)
        var actual = new Date()
        var elapsed = new Date(actual - readed)
        
        function roundToHour(date){
        	var p = 60*60*1000;
        	return new Date(Math.round(date.getTime() /p)*p)
        }
        
        var elapsedAround = roundToHour(elapsed);
        
        mytextfield.text = elapsedAround.getHours()
        

        It works, but only when reded timestamp and actual date is in the same day.... What I should change to calculate hours when readed timestamp is from previus day ?

        piervalliP Offline
        piervalliP Offline
        piervalli
        wrote on last edited by
        #2

        @Damian7546

        You can use use.

        console.time("wholeFunction");
        console.timeEnd("wholeFunction");

        https://doc.qt.io/qt-6/qtquick-debugging.html

        ? D 2 Replies Last reply
        0
        • piervalliP piervalli

          @Damian7546

          You can use use.

          console.time("wholeFunction");
          console.timeEnd("wholeFunction");

          https://doc.qt.io/qt-6/qtquick-debugging.html

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • piervalliP piervalli

            @Damian7546

            You can use use.

            console.time("wholeFunction");
            console.timeEnd("wholeFunction");

            https://doc.qt.io/qt-6/qtquick-debugging.html

            D Offline
            D Offline
            Damian7546
            wrote on last edited by
            #4

            @piervalli I am still don't understand ....

            piervalliP 1 Reply Last reply
            0
            • D Damian7546

              @piervalli I am still don't understand ....

              piervalliP Offline
              piervalliP Offline
              piervalli
              wrote on last edited by piervalli
              #5

              @Damian7546
              If you need to calculate diff date, you can use a simple js algoritme.
              You can use a simple js date

              function inDays (d1, d2) {
                          var t2 = d2.getTime();
                          var t1 = d1.getTime();
              
                          return Math.floor((t2-t1)/(24*3600*1000));
                      }
               function inHours (d1, d2) {
                          var t2 = d2.getTime();
                          var t1 = d1.getTime();
              
                          return Math.floor((t2-t1)/(3600*1000));
                      }
              
              Component.onCompleted:{
                      var d1 = new Date()
                      var d2 = new Date(2023,3,1)
                      console.error("inDays",inDays(d1,d2))
                  }
              

              Olther examples are in the link of stackoverflow, for hours

              Instanded console.time and console.timeEnd is used to calculated diff time in a function (benchmark)

              https://stackoverflow.com/questions/7763327/how-to-calculate-date-difference-in-javascript

              1 Reply Last reply
              0
              • D Damian7546 has marked this topic as solved on

              • Login

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