Count days between two dates

I tried to make a program with Qt that counts how many days between two dates. The problem is that I am new to Qt and I have no job.

I think QDateTime is simple, but I do not understand the structure of the program.

Can someone please give me an example. Just a simple program that shows how many days it is before Christmas, for example.

+4
source share
2 answers

Your problem is very simple.

Create a console application in QtCreator and edit your main.cpp as follows:

 #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 dEndOfTheWord(2012, 12, 21); qDebug() << "Today is" << dNow.toString("dd.MM.yyyy") << "Days to end of the world: " << dNow.daysTo(dEndOfTheWord); return a.exec(); } 

And you get the output, for example:

Today is "12/18/2012" Days until the end of the world: 3

PS But my advice teaches you C ++ (add The Definitive C ++ Book Guide and List to your favorite topic) and then learn Qt (I recommend programming in C ++ GUI with Qt 4 from Jasmin Blanchette and Mark Summerfield and other Summerfields books). Good luck

+16
source

You will need to use

 qint64 QDateTime::toMSecsSinceEpoch () const 

Returns the date and time as milliseconds since 1970-01-01 00: 00: 00.000

Since there is no way to directly find timeSpan. Convert 2 dateTime objects to milliseconds, subtract and convert them to days, hours, minutes, seconds using mathematical manipulation.

0
source

All Articles