Time span between two QDateTime
-
wrote on 17 Sept 2011, 16:30 last edited by
how to get the difference between two QDateTime in milliseconds (I want the result to be in DateFormat)
thanks a lot -
wrote on 17 Sept 2011, 18:11 last edited by
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.
-
wrote on 17 Sept 2011, 18:52 last edited by
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? -
wrote on 17 Sept 2011, 19:04 last edited by
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.
-
wrote on 17 Sept 2011, 20:41 last edited by
thanks andre!!!
how can I use it, I mean should I download only the .cpp and .h or the whole thing
thanks in advance -
wrote on 17 Sept 2011, 21:02 last edited by
koahnig>> yes exactly, I need it in a time format
-
wrote on 17 Sept 2011, 21:22 last edited by
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!
-
wrote on 18 Sept 2011, 07:06 last edited by
Note that QTimeSpan uses QDateTime::msecsTo internally. That functionality may already be enough for you.
-
wrote on 5 Jun 2013, 05:51 last edited by
Is there any way to calculate time span between two QDateTimes using Qt version 4.8.4 ?
-
wrote on 5 Jun 2013, 06:31 last edited by
[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.
-
wrote on 12 Mar 2014, 19:13 last edited by
You can use a third-party class TimeDelta for Qt:
https://github.com/cardinalby/time_delta