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 Updated to NodeBB v4.3 + New Features

QDate, QTime & QDateTime functions

Scheduled Pinned Locked Moved Solved General and Desktop
qdateqtimeqdatetime
14 Posts 5 Posters 2.2k Views 1 Watching
  • 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.
  • Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 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
    3
    • Christian EhrlicherC Christian Ehrlicher

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

      JonBJ 1 Reply Last reply
      1
      • B Bracha

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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.HilkJ B 2 Replies Last reply
        1
        • JonBJ JonB

          @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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on 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.

          JonBJ 1 Reply Last reply
          1
          • JonBJ JonB

            @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 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.HilkJ J.Hilk

              @JonB or, hear me out,

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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
              0
              • JonBJ JonB

                @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 last edited by
                #11

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

                1 Reply Last reply
                0
                • J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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
                  • Christian EhrlicherC Christian Ehrlicher

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

                      • Login

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