Search code examples
c++qtqt4

How to put a current date in QLabel in digits?


I have a problem with convert a current date. I want to put a current date in a QLabel but i can't put a QDate type.

 QDate today;
      today=QDate::currentDate();
        datamiasto->setText("Data wystawienia: "+today+", xyz");

I can put it when i convert it to string but when i do it date is not in digits. How can i put a digit date in a QLabel ?


Solution

  • Try this:

    QDateTime dateTime = dateTime.currentDateTime();
    QString dateTimeString = dateTime.toString("yyyy-MM-dd_hh-mm-ss");
    

    QDateTime::toString(const QString &format) : Returns the datetime as a string. The format parameter determines the format of the result string. (give your custom format to the function)

    QDateTime::currentDateTime() : Returns the current datetime, as reported by the system clock, in the local time zone.