How to get the difference between two QDateTimes in milliseconds?

I would probably use a.daysTo(b)*1000*60*60*24 + a.time().msecsTo(b.time()). Note that you need to watch how close you can be, since you're going to overflow your data type rather quickly.


I realize that this question is from 2010, and that Qt 4.7 didn't exist back then (it actually came out about a week after this question was originally asked--September 21 2010), but for people who are looking for how to do this now:

As of Qt 4.7, QDateTime has a "msecsTo" method. See Qt 4.8 documentation at http://doc.qt.io/qt-4.8/qdatetime.html#msecsTo.

QDateTime dateTime1 = QDateTime::currentDateTime();
// let's say exactly 5 seconds pass here...
QDateTime dateTime2 = QDateTime::currentDateTime();
qint64 millisecondsDiff = dateTime1.msecsTo(dateTime2);
// millisecondsDiff is equal to 5000

Tags:

Qt

Qt4