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
QtWS25 Last Chance

QDate, QTime & QDateTime functions

Scheduled Pinned Locked Moved Solved General and Desktop
qdateqtimeqdatetime
14 Posts 5 Posters 1.6k 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.
  • B Offline
    B Offline
    Bracha
    wrote on 16 Jul 2022, 20:26 last edited by
    #1

    Hi,
    I am working on program with many function relate to date & time,
    and I see that is no function to set units of date/time, like setMonth, setHour.
    Every time I want to change a single item I need to build/update a temporary object of date/time and use setDate/SetHMS.
    The code is much more cumbersome, and less readable.
    As well as calls to the constructor and the creation of unnecessary objects that take up space in memory.
    To me it is basic functions, and it is really requested that this be possible.
    What can be done and acted upon for this?
    Waiting to hear your opinion on the matter as well as guidance on action for creating these functions.
    Thanks.

    C 1 Reply Last reply 16 Jul 2022, 20:31
    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
  • C Offline
    C Offline
    Christian Ehrlicher Lifetime Qt Champion
    replied to Bracha on 16 Jul 2022, 20:31 last edited by
    #2

    @Bracha said in QDate, QTime & QDateTime functions:

    Every time I want to change a single item

    What's your usecase for this? Creating a QDate/QTime is not that expensive.

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

    1 Reply Last reply
    1
  • A Offline
    A Offline
    Axel Spoerl Moderators
    wrote on 16 Jul 2022, 20:41 last edited by
    #3

    In addition to @Christian-Ehrlicher: you can define macros to make your code easier to be read.

    Software Engineer
    The Qt Company, Oslo

    1 Reply Last reply
    1
  • B Offline
    B Offline
    Bracha
    wrote on 17 Jul 2022, 15:16 last edited by Bracha
    #4

    @Christian-Ehrlicher
    Not necessarily expensive as well as cumbersome.
    I have some conditions and I need change year/month/day accordingly, and i need to setDate with the parameters that already appear,
    like: qDate_tmp.setDate(qDateTime_L.date().year() + 1, qDateTime_L.date().month(),
    qDateTime_L.date().day());
    qDateTime_L.setDate(qDate_tmp);
    instead: qDateTime_L.setYear(qDateTime_L.date().year() + 1)

    @Axel-Spoerl
    I don't like use Macro's

    1 Reply Last reply
    0
  • 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
  • B Offline
    B Offline
    Bracha
    replied to Christian Ehrlicher 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
  • J Offline
    J Offline
    JonB
    replied to Bracha 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 Offline
    J Offline
    J.Hilk Moderators
    replied to JonB 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
  • B Offline
    B Offline
    Bracha
    replied to JonB 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 Offline
    J Offline
    JonB
    replied to J.Hilk 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
  • B Offline
    B Offline
    Bracha
    replied to JonB 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
  • B Offline
    B Offline
    Bracha
    replied to Christian Ehrlicher 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

8/14

18 Jul 2022, 12:02

6 unread
  • Login

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