Time span between two QDateTime
-
There is no timespan class in Qt yet. "Andre":http://developer.qt.nokia.com/member/438 and "ZapB":http://developer.qt.nokia.com/member/3246 have developed one and sent a merge request to Nokia. I don't know the current state of this, though.
You can calculate the difference with "QDateTime::toMSecsSinceEpoch() ":http://doc.qt.nokia.com/4.7/qdatetime.html#toMSecsSinceEpoch, but you will have to do the formatting yourself for the time being.
-
In principle you could use "setMSecsSinceEpoch":http://doc.qt.nokia.com/4.7/qdatetime.html#setMSecsSinceEpoch and store the difference into QDateTime, but what for. This makes no sense. Probably nobody can interpret the outcome.
Do you mean time format? -
QTimeSpan was meant to go into Qt 4.8, but did not make it unfortunately. Because the time/date stuff is undergoing a rewrite for Qt 5, QTimeSpan will also need to be redone. However, the code is there, and works. It will allow you to just do:
@
QTimeSpan span = lastDate - firstDate;
qDebug() << span.toMilliseconds(); //and many more ways of doing it
@Everything is "available":https://qt.gitorious.org/qt/qt/merge_requests/1014 on Gitorious.
-
A time format in its current state in Qt represents a time of a day. Naturally, this is restricted from 00:00:00 to 23:59:59. You do not seem to want this semantic. A time span can last an arbitrarily long time, years, decades, centennials if you want. This is not doable in a time format if your timespan is longer than a day. And even if it is less, using a QTime (or any other time class that's made for time-of-day) is cumbersome, error prone and abusive at best.
To include the code into your own project, you need qtimespan.h, qtimespan.cpp and maybe you want to move the enum TimeSpanUnit into qtimespan.h. Make sure to change the name of the class to something different thant QTimeSpan in order to prepare for the possible inclusion of the merge request!
-
[quote author="Anticross" date="1370411488"]Is there any way to calculate time span between two QDateTimes using Qt version 4.8.4 ?[/quote]
If I understand the question correctly then yes, use the "QDateTime::msecsTo":http://qt-project.org/doc/qt-4.8/qdatetime.html#msecsTo function to get the difference. You can then use the ms and convert it to a Date format that you need.
-
You can use a third-party class TimeDelta for Qt:
https://github.com/cardinalby/time_delta