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. QDate, QTime & QDateTime functions
Forum Update on Monday, May 27th 2025

QDate, QTime & QDateTime functions

Scheduled Pinned Locked Moved Solved General and Desktop
qdateqtimeqdatetime
14 Posts 5 Posters 1.7k 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 17 Jul 2022, 18:25 last edited by
    #5

    What's wrong with QDate::addYears()?

    auto newDate = QDateTime(curDateTime.date().addYear(1), curDateTime.time());
    or
    curDateTime.setDate(curDateTime.date().addYear(1));

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    B 2 Replies Last reply 18 Jul 2022, 11:27
    3
    • C Christian Ehrlicher
      17 Jul 2022, 18:25

      What's wrong with QDate::addYears()?

      auto newDate = QDateTime(curDateTime.date().addYear(1), curDateTime.time());
      or
      curDateTime.setDate(curDateTime.date().addYear(1));

      B Offline
      B Offline
      Bracha
      wrote on 18 Jul 2022, 11:27 last edited by
      #6

      @Christian-Ehrlicher
      Thank you,
      I'll try to play with it and see how it works .
      It's still not as simple and bright as the option I suggested but if that's what it is, we'll get it right.
      Thank you all.

      J 1 Reply Last reply 18 Jul 2022, 11:56
      1
      • B Bracha
        18 Jul 2022, 11:27

        @Christian-Ehrlicher
        Thank you,
        I'll try to play with it and see how it works .
        It's still not as simple and bright as the option I suggested but if that's what it is, we'll get it right.
        Thank you all.

        J Online
        J Online
        JonB
        wrote on 18 Jul 2022, 11:56 last edited by
        #7

        @Bracha
        Yes it is true that it's a bit awkward to want to e.g. just set one element in an existing QDateTime. It does not help that their are methods on QDate & QTime which help but not on a whole QDateTime. You just have to live with the way Qt does this.

        If it were me I would probably write a global "utility" inline function to do a particular operation you want on a QDateTime, for the code you showed.

        J B 2 Replies Last reply 18 Jul 2022, 12:02
        1
        • J JonB
          18 Jul 2022, 11:56

          @Bracha
          Yes it is true that it's a bit awkward to want to e.g. just set one element in an existing QDateTime. It does not help that their are methods on QDate & QTime which help but not on a whole QDateTime. You just have to live with the way Qt does this.

          If it were me I would probably write a global "utility" inline function to do a particular operation you want on a QDateTime, for the code you showed.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 18 Jul 2022, 12:02 last edited by
          #8

          @JonB or, hear me out,

          you make a new class and inherit from QDateTime where you add those convenient setX functions....


          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.

          J 1 Reply Last reply 18 Jul 2022, 12:35
          1
          • J JonB
            18 Jul 2022, 11:56

            @Bracha
            Yes it is true that it's a bit awkward to want to e.g. just set one element in an existing QDateTime. It does not help that their are methods on QDate & QTime which help but not on a whole QDateTime. You just have to live with the way Qt does this.

            If it were me I would probably write a global "utility" inline function to do a particular operation you want on a QDateTime, for the code you showed.

            B Offline
            B Offline
            Bracha
            wrote on 18 Jul 2022, 12:26 last edited by
            #9

            @JonB and @J-Hilk
            Thank you friends for your understanding and suggestions,
            I will seriously consider them and use them.

            1 Reply Last reply
            1
            • J J.Hilk
              18 Jul 2022, 12:02

              @JonB or, hear me out,

              you make a new class and inherit from QDateTime where you add those convenient setX functions....

              J Online
              J Online
              JonB
              wrote on 18 Jul 2022, 12:35 last edited by
              #10

              @J-Hilk said in QDate, QTime & QDateTime functions:

              you make a new class and inherit from QDateTime where you add those convenient setX functions....

              But that does not work when existing functions return a QDateTime..... :(

              B 1 Reply Last reply 18 Jul 2022, 13:40
              0
              • J JonB
                18 Jul 2022, 12:35

                @J-Hilk said in QDate, QTime & QDateTime functions:

                you make a new class and inherit from QDateTime where you add those convenient setX functions....

                But that does not work when existing functions return a QDateTime..... :(

                B Offline
                B Offline
                Bracha
                wrote on 18 Jul 2022, 13:40 last edited by
                #11

                @JonB
                Will have to convert them to the new object
                Another type of awkwardness

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 18 Jul 2022, 13:51 last edited by
                  #12

                  you could, and should, make a constructor that excepts a QDateTime object as argument, that than can be done implicitly :D


                  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
                  1
                  • C Christian Ehrlicher
                    17 Jul 2022, 18:25

                    What's wrong with QDate::addYears()?

                    auto newDate = QDateTime(curDateTime.date().addYear(1), curDateTime.time());
                    or
                    curDateTime.setDate(curDateTime.date().addYear(1));

                    B Offline
                    B Offline
                    Bracha
                    wrote on 18 Jul 2022, 14:26 last edited by
                    #13

                    @Christian-Ehrlicher I actually used your suggestion which is convenient for me at the moment but is probably a solution only for the date and not the time, Just want you to pay attention

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      Bracha
                      wrote on 18 Jul 2022, 14:28 last edited by Bracha
                      #14

                      Just want to summarize the proposed solutions in an orderly fashion so that it remains for the future:

                      @Christian-Ehrlicher: use QDate::addDays() / QDate::addMonths() / QDate::addYears(). [for dates]
                      example: [qDateTime.setDate(qDateTime.date().addYears(XXX)]

                      @JonB : write a global "utility" inline function to do a particular operation you want on a QDateTime.

                      @J-Hilk: make a new class and inherit from QDateTime where you add those convenient setX functions.
                      don't forget to convert the returning objects from the existing functions that return a QDateTime object to the new object,
                      or make a constructor that excepts a QDateTime object as argument, that than can be done implicitly.

                      Thank you all for a fruitful discussion and varied and helpful suggestions!

                      1 Reply Last reply
                      0

                      14/14

                      18 Jul 2022, 14:28

                      • Login

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