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. Time span between two QDateTime

Time span between two QDateTime

Scheduled Pinned Locked Moved General and Desktop
11 Posts 7 Posters 24.1k Views
  • 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.
  • K Offline
    K Offline
    kyoti
    wrote on 17 Sept 2011, 16:30 last edited by
    #1

    how to get the difference between two QDateTime in milliseconds (I want the result to be in DateFormat)
    thanks a lot

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 17 Sept 2011, 18:11 last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on 17 Sept 2011, 18:52 last edited by
        #3

        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?

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 17 Sept 2011, 19:04 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kyoti
            wrote on 17 Sept 2011, 20:41 last edited by
            #5

            thanks andre!!!
            how can I use it, I mean should I download only the .cpp and .h or the whole thing
            thanks in advance

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kyoti
              wrote on 17 Sept 2011, 21:02 last edited by
              #6

              koahnig>> yes exactly, I need it in a time format

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on 17 Sept 2011, 21:22 last edited by
                #7

                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!

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on 18 Sept 2011, 07:06 last edited by
                  #8

                  Note that QTimeSpan uses QDateTime::msecsTo internally. That functionality may already be enough for you.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Anticross
                    wrote on 5 Jun 2013, 05:51 last edited by
                    #9

                    Is there any way to calculate time span between two QDateTimes using Qt version 4.8.4 ?

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      TheBadger
                      wrote on 5 Jun 2013, 06:31 last edited by
                      #10

                      [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.


                      Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        cardinalby
                        wrote on 12 Mar 2014, 19:13 last edited by
                        #11

                        You can use a third-party class TimeDelta for Qt:
                        https://github.com/cardinalby/time_delta

                        1 Reply Last reply
                        0

                        • Login

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