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. Difference between two QDateTime in milliseconds.
Qt 6.11 is out! See what's new in the release blog

Difference between two QDateTime in milliseconds.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 10.4k 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.
  • srikanthS Offline
    srikanthS Offline
    srikanth
    wrote on last edited by
    #1

    my ITime.cpp file

    
    #include "ITime.h"
    
    
    ITime::ITime()
    {
        
    }
    
    ITime::~ITime()
    {
    	
    }
    
    void ITime::start()
    {	
       oStartTime = QDateTime::currentDateTime();
       
    }
    
    
    quint64 ITime::elapsed()
    {
    	
    	QDateTime otime = oStartTime;
    	QString str = otime.toString();
    	oEndTime = QDateTime::currentDateTime();
    quint64 elapsed = 0;
    elapsed = (oEndTime.secsTo(otime));
    QString str1 = QString::number(elapsed);
    elapsed = (elapsed * 1000);
    
    
    return elapsed;
    }
    

    my main.cpp file contains

    #include "ITime.h"
    
    int main()
    {
    
    ITime time;
    time.start();
     qDebug() << "current date" << time.oStartTime ;
    time.elapsed();
    qDebug() << "old date" << time.oEndTime ;
    
    qDebug() << "differnce time" << time.elapsed() ;
    
    }
    

    my ITime.h file contains

    #ifndef ITime_H
    #define ITime_H
    #include <QDateTime>
    #include <QDebug> 
    #include <iostream>
    
    
    class ITime 
    {
    public:
    	ITime();
    	~ITime();
    	void start();
    	quint64 elapsed();
    
    
    public:
    
    	int nTimeDiff;
    	QDateTime oStartTime;
    	QDateTime oEndTime;
    	
    
    };
    #endif // ITime_H
    
    

    here am trying to get the difference between two DateTime intervals but the difference between two time in not getting correct output

    problem is getting here

    quint64 elapsed = 0;
    elapsed = (oEndTime.secsTo(otime));
    QString str1 = QString::number(elapsed);
    elapsed = (elapsed * 1000);

    am struck here please help me to get out of this

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You're kinda re-inventing the wheel. There's QElapsedTimer for this case.

      1 Reply Last reply
      3
      • S Offline
        S Offline
        Sagar Ojha
        wrote on last edited by
        #3

        I need the same in minuts.

        Chris KawaC 1 Reply Last reply
        0
        • S Sagar Ojha

          I need the same in minuts.

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Sagar-Ojha Well... I hope you know how to "convert" milliseconds to minutes ;)

          S 1 Reply Last reply
          4
          • Chris KawaC Chris Kawa

            @Sagar-Ojha Well... I hope you know how to "convert" milliseconds to minutes ;)

            S Offline
            S Offline
            Sagar Ojha
            wrote on last edited by
            #5

            @Chris-Kawa obviously

            1 Reply Last reply
            0
            • srikanthS Offline
              srikanthS Offline
              srikanth
              wrote on last edited by srikanth
              #6

              i want the difference between two DateTime in milliseconds

              for that

              QDateTime oStartTime = QDateTime::currentDateTime(); 
              \\\ 30 milliseconds difference.	
              QDateTime oEndtTime = QDateTime::currentDateTime();
              

              now am trying like this to get difference in milliseconds

              quint64 difference= (oStartTime.secsTo(oEndtTime )*1000);
              

              the result is "0" because here we are converting into second's so am not getting exact output to get difference in milliseconds

              am using another method

              quint64 difference= (oStartTime.time().msecsTo(oEndtTime .time()));
              

              here am getting exact time difference in milliseconds but if we change the date for oEndtTime the the difference of days are not coming , because time() is limited to 24 hours only , if i want to get DateTime difference in more than 1 day then time() is not useful , please help me to get out of this

              am struck here to convert DateTime difference in milliseconds .

              VRoninV 1 Reply Last reply
              0
              • srikanthS srikanth

                i want the difference between two DateTime in milliseconds

                for that

                QDateTime oStartTime = QDateTime::currentDateTime(); 
                \\\ 30 milliseconds difference.	
                QDateTime oEndtTime = QDateTime::currentDateTime();
                

                now am trying like this to get difference in milliseconds

                quint64 difference= (oStartTime.secsTo(oEndtTime )*1000);
                

                the result is "0" because here we are converting into second's so am not getting exact output to get difference in milliseconds

                am using another method

                quint64 difference= (oStartTime.time().msecsTo(oEndtTime .time()));
                

                here am getting exact time difference in milliseconds but if we change the date for oEndtTime the the difference of days are not coming , because time() is limited to 24 hours only , if i want to get DateTime difference in more than 1 day then time() is not useful , please help me to get out of this

                am struck here to convert DateTime difference in milliseconds .

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7
                
                quint64 difference = qAbs(oStartTime.date().daysTo(oEndtTime .date()));
                difference  *= static_cast<quint64>(24); // days to hours
                difference  *= static_cast<quint64>(60); // hours to minutes
                difference  *= static_cast<quint64>(60); // minutes to seconds
                difference  *= static_cast<quint64>(1000); // seconds to milliseconds
                difference += qAbs(oStartTime.time().msecsTo(oEndtTime .time()));
                

                "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
                2

                • Login

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