6

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.

NG_
  • 6,895
  • 7
  • 45
  • 67
Sep
  • 91
  • 1
  • 1
  • 2

2 Answers2

21

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!

NG_
  • 6,895
  • 7
  • 45
  • 67
0

You will have to use

qint64 QDateTime::toMSecsSinceEpoch () const

This returns the datetime as the number of milliseconds that have passed since 1970-01-01 00:00:00.000

As there is no way to directly find timeSpan. Convert your 2 dateTime objects into milliseconds, subtract and convert into days, hours, mins, secs using mathematical manipulation.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • Thanks! But my main problem is the structure of program. The various different files and such. How to make the program with qtcreator for example to Nokia N9... – Sep Jun 25 '12 at 10:57