Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting days between two dates

Tags:

qt

qdatetime

I tried to make a program with Qt that counts how many days are between two dates. The problem is that I am novice in Qt and I haven't got it working.

I guess QDateTime is easy, but I don't understand the structure of a program.

Could somebody please make an example for me. Just a simple program that shows how many days it is until Christmas for example.

like image 885
Sep Avatar asked Jun 25 '12 10:06

Sep


People also ask

How do you calculate days from two dates?

To find the number of days between these two dates, you can enter “=B2-B1” (without the quotes into cell B3). Once you hit enter, Excel will automatically calculate the number of days between the two dates entered.

How do I calculate the number of working days between two dates in Excel?

How to Calculate Working Days in Excel. The NETWORKDAYS Function[1] calculates the number of workdays between two dates in Excel. When using the function, the number of weekends are automatically excluded. It also allows you to skip specified holidays and only count business days.


1 Answers

Your problem is very simple.

Create console application in QtCreator, and edit your main.cpp this way:

#include <QApplication>
#include <QDate>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // get current date
    QDate dNow(QDate::currentDate());
    // create other date
    //  by giving date 12.21.2012 (joke about end of the world)
    QDate dEndOfTheWorld(2012, 12, 21);
    qDebug() << "Today is" << dNow.toString("dd.MM.yyyy")
             << "Days to end of the world: "
             << dNow.daysTo(dEndOfTheWorld);

    return a.exec();
}

And you'll got output like:

Today is "18.12.2012" Days to end of the world: 3

P.S. But my advice to you is learn C++ (add to your favorite this topic -- The Definitive C++ Book Guide and List), and then learn Qt (I recommend C++ GUI Programming with Qt 4 by Jasmin Blanchette & Mark Summerfield and Summerfields other books). Good luck!

like image 117
tro Avatar answered Sep 23 '22 23:09

tro