Solved Difference between two QDateTime in milliseconds.
-
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
-
You're kinda re-inventing the wheel. There's QElapsedTimer for this case.
-
I need the same in minuts.
-
@Sagar-Ojha Well... I hope you know how to "convert" milliseconds to minutes ;)
-
@Chris-Kawa obviously
-
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 .
-
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()));