1

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

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Dhivya Prabha
  • 77
  • 2
  • 8
  • You have to distinguish between internal organisation of objects and their (printable) representation. QDate is for computation - if you want it shown somewhere: use formatting means. – ngulam Apr 03 '15 at 11:34

2 Answers2

4

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().

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0

you can use datetime.strftime()

yourdate.strftime('%Y, %m, %d')
Doc
  • 5,078
  • 6
  • 52
  • 88