Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert QDate to Qstring?

Tags:

c++

string

date

qt

I have trouble converting QDate to QString. See the code below:

cout << QString("\nBooking Date: %1").arg(QDate::toString(this->getBookingDate() &YYYY.mm.dd)

When I try to compile this it gives me:

‘YYYY’ was not declared in this scope

Yet the documentation says:

"Returns the date as a string. The format parameter determines the format of the result string..." and goes on to list the format strings.

I am new to Qt so if I am missing the obvious please excuse the ignorance.

like image 519
Letholdrus Avatar asked Mar 15 '12 19:03

Letholdrus


People also ask

How to use QDate in Qt?

QDate QDate::fromString ( const QString & s, Qt::DateFormat f = Qt::TextDate ) [static] Returns the QDate represented by the string s, using the format f, or an invalid date if the string cannot be parsed. Note for Qt::TextDate: It is recommended that you use the English short month names (e.g. "Jan").

How do you use QDate?

QDate::QDate(int y, int m, int d) Constructs a date with year y, month m and day d. The date is understood in terms of the Gregorian calendar. If the specified date is invalid, the date is not set and isValid() returns false .

How can I get current date and time in Qt?

[static] QDateTime QDateTime::currentDateTime() Returns the current datetime, as reported by the system clock, in the local time zone. See also currentDateTimeUtc(), QDate::currentDate(), QTime::currentTime(), and toTimeSpec().


2 Answers

You want this->getBookingDate().toString("yyyy.MM.dd"). For further info, read the documentation.

like image 127
jpm Avatar answered Oct 11 '22 01:10

jpm


in PySide2 you can use this:

self.dateEdit.date().toString("yyyy.MM.dd")
like image 21
Rajiv Sharma Avatar answered Oct 11 '22 00:10

Rajiv Sharma