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. how to find date time diffrence between date time in format "yyyy-mm-dd hh:mm:ss" using QDateTime ?
QtWS25 Last Chance

how to find date time diffrence between date time in format "yyyy-mm-dd hh:mm:ss" using QDateTime ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 14 Nov 2022, 06:33 last edited by Qt embedded developer
    #1

    Can any body help me to get current date time in format like yyyy-mm-dd hh:mm:ss for example :"2022-11-14 09:34:07".

    i want to also find difference between current date time and older date time. so if anybody know how to find it then please let me know with example.

    1 Reply Last reply
    0
    • Q Qt embedded developer
      14 Nov 2022, 06:55

      @Bonnie Hi want to find difference between date time

      i.e. difference between 2022-11-14 09:34:07 and 2022-11-14 12:17:54

      how many years, months, days, hours , seconds and millisecond get spent between this dates ?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 14 Nov 2022, 07:05 last edited by
      #6

      @Qt-embedded-developer

           QDateTime dt1 = QDateTime::fromString("2022-11-14 09:34:07", "yyyy-MM-dd hh:mm:ss");
           QDateTime dt2 = QDateTime::fromString("2022-11-14 12:17:54", "yyyy-MM-dd hh:mm:ss");
           
           qint64 secondsbetweenDateTime = dt1.secsTo(dt2);
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      5
      • Q Offline
        Q Offline
        Qt embedded developer
        wrote on 14 Nov 2022, 06:49 last edited by
        #2
        QDateTime date = QDateTime::currentDateTime();
            QString formattedTime = date.toString("yyyy-MM-dd hh:mm:ss");
        
        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on 14 Nov 2022, 06:49 last edited by Bonnie
          #3
          1. Edit: sorry wrong understanding, I see you've already figured it out.
          2. What kind of difference do you want? There is QDateTime::secsTo that returns the number of seconds from a datetime to another
          Q 1 Reply Last reply 14 Nov 2022, 06:55
          0
          • B Bonnie
            14 Nov 2022, 06:49
            1. Edit: sorry wrong understanding, I see you've already figured it out.
            2. What kind of difference do you want? There is QDateTime::secsTo that returns the number of seconds from a datetime to another
            Q Offline
            Q Offline
            Qt embedded developer
            wrote on 14 Nov 2022, 06:55 last edited by
            #4

            @Bonnie Hi want to find difference between date time

            i.e. difference between 2022-11-14 09:34:07 and 2022-11-14 12:17:54

            how many years, months, days, hours , seconds and millisecond get spent between this dates ?

            B J 2 Replies Last reply 14 Nov 2022, 07:04
            0
            • Q Qt embedded developer
              14 Nov 2022, 06:55

              @Bonnie Hi want to find difference between date time

              i.e. difference between 2022-11-14 09:34:07 and 2022-11-14 12:17:54

              how many years, months, days, hours , seconds and millisecond get spent between this dates ?

              B Offline
              B Offline
              Bonnie
              wrote on 14 Nov 2022, 07:04 last edited by
              #5

              @Qt-embedded-developer As I posted, there are secsTo in QDateTime, and also msecsTo and daysTo.
              Others you would need to calculate them by yourself depending on the results of the above functions.

              1 Reply Last reply
              2
              • Q Qt embedded developer
                14 Nov 2022, 06:55

                @Bonnie Hi want to find difference between date time

                i.e. difference between 2022-11-14 09:34:07 and 2022-11-14 12:17:54

                how many years, months, days, hours , seconds and millisecond get spent between this dates ?

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 14 Nov 2022, 07:05 last edited by
                #6

                @Qt-embedded-developer

                     QDateTime dt1 = QDateTime::fromString("2022-11-14 09:34:07", "yyyy-MM-dd hh:mm:ss");
                     QDateTime dt2 = QDateTime::fromString("2022-11-14 12:17:54", "yyyy-MM-dd hh:mm:ss");
                     
                     qint64 secondsbetweenDateTime = dt1.secsTo(dt2);
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                5

                4/6

                14 Nov 2022, 06:55

                • Login

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