Change the format of a QDate

7k Views Asked by At

I need to change the format of a QDate. Here is my code:

 yday = (QtCore.QDate.currentDate().addDays(-1))

And I got this result...

PyQt4.QtCore.QDate(2015, 4, 2)

But I need the date in this format:

2015/04/03

2

There are 2 best solutions below

0
On BEST ANSWER

you can use datetime.strftime()

yourdate.strftime('%Y, %m, %d')
0
On

A QDate can be converted to a string using its toString method:

>>> yday  = QtCore.QDate.currentDate().addDays(-1)
>>> yday.toString()
'Thu Apr 2 2015'
>>> yday.toString(QtCore.Qt.ISODate)
'2015-04-02'
>>> yday.toString('yyyy/MM/dd')
'2015/04/02'

Note that this output is from Python3. If you're using Python2, by default, the output will be a QString - but it can be converted to a python string using unicode().