I have this code, which, in my opinion, should tell me the time of the computer.

#include <QTextStream> #include <QDate> #include <QTime> int main(void) { QTextStream out(stdout); QDate dt2; dt2.setDate(QDateTime::currentDateTime()); out << dt2.toString("yyyy/MM/dd") << endl; } 

Unfortunately, this code does not work. where is my mistake?

    2 answers 2

     QDateTime dt2 = QDateTime::currentDateTime(); qDebug() << dt2.toString("yyyy/MM/dd"); 

    Or as a shorter option:

     qDebug()<<QDateTime::currentDateTime().toString("yyyy/MM/dd"); 

    I add that the above code does not compile, the QDate::setDate accepts int year, int month, int day for input, and you are trying to pass an object of QDateTime class. The date and time in Qt is described by the QDateTime class, so if you need to store the date and time in one entity, I recommend using it.

       #include <QTextStream> #include <QDate> #include <QTime> int main(void) { QTextStream out(stdout); QDate dt2 = QDate::currentDate(); out << dt2.toString("yyyy/MM/dd"); } 

      For the future: write more details here is your "code does not work." It is necessary to clarify: not compiled; compiles, but displays not what you need, clarify what outputs and what you want to get.