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. Get dates between two end dates
QtWS25 Last Chance

Get dates between two end dates

Scheduled Pinned Locked Moved Solved General and Desktop
qdatetimeqt5desktop
6 Posts 4 Posters 1.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.
  • A Offline
    A Offline
    Andrex_Qt
    wrote on 5 Feb 2020, 10:33 last edited by
    #1

    HI,
    I have been working on a program which will display all the dates between a Start date and an End date.
    So far, I haven't been able to come up with logic or find solution. We can use daysto(QDate) to get number of days for other date that's clear.
    can someone help me with this?
    Thanks in Advance.

    J 1 Reply Last reply 5 Feb 2020, 10:38
    0
  • J Online
    J Online
    J.Hilk Moderators
    replied to Andrex_Qt on 5 Feb 2020, 10:38 last edited by
    #2

    @Andrex_Qt

    I fail to see the difficulty, what did you try so far ?

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QDate d1 = QDate::currentDate();
        QDate d2 = d1.addYears(1);
    
        QVector<QDate> dateList;
    
        for(int i(0); i <d1.daysTo(d2); i++)
            dateList.append(d1.addDays(i));
    
        for(const QDate & d: dateList)
            qDebug() << d;
    
        return a.exec();
    }
    

    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.

    A A 2 Replies Last reply 5 Feb 2020, 11:15
    6
  • A Offline
    A Offline
    aha_1980 Lifetime Qt Champion
    replied to J.Hilk on 5 Feb 2020, 11:15 last edited by aha_1980 2 May 2020, 11:15
    #3

    @J-Hilk said in Get dates between two end dates:

    for(const QDate & d: dateList)

    Ehem... better use:

    for (const QDate &d : qAsConst(dateList))

    as range for on non-const Qt container might be expensive.

    Btw: for(int i(0); i <d1.daysTo(d2); i++): it might be better to calculate d1.daysTo(d2) outside the comparison once.

    Regards

    Qt has to stay free or it will die.

    J 1 Reply Last reply 5 Feb 2020, 11:18
    4
  • J Online
    J Online
    J.Hilk Moderators
    replied to aha_1980 on 5 Feb 2020, 11:18 last edited by J.Hilk 2 May 2020, 11:19
    #4

    @aha_1980
    alt text

    😝

    sure

     for(int i(0), j(d1.daysTo(d2)); i <j; i++)
            dateList.append(d1.addDays(i));
    
        for(const QDate & d: qAsConst(dateList))
            qDebug() << 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.

    J 1 Reply Last reply 5 Feb 2020, 11:23
    5
  • J Online
    J Online
    JonB
    replied to J.Hilk on 5 Feb 2020, 11:23 last edited by
    #5

    @J-Hilk That's a neat pic :)

    1 Reply Last reply
    2
  • A Offline
    A Offline
    Andrex_Qt
    replied to J.Hilk on 5 Feb 2020, 14:16 last edited by
    #6

    @J-Hilk @aha_1980
    I think i failed to see simplicity of this one.
    thank you for guidence.

    1 Reply Last reply
    3

6/6

5 Feb 2020, 14:16

  • Login

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