1

I have a question about PyQt4. I have a date that is of a type QDate and I want to simply turn it into a string format as opposed to QDate format. For example, if the date is 09/16/2013, I would want to change it into a string form of September 16, 2013 if possible.

I played around with toString but I think that only works with C++ (unless I am mistaken).

user1871869
  • 3,317
  • 13
  • 56
  • 106

1 Answers1

5

Documentation is your friend ;)

>>> date = QtCore.QDate.fromString('20130916', 'yyyyMd')

# PySide
>>> date.toString('MMMM d, yyyy')
u'September 16, 2013'

# PyQt4
>>> date.toString('MMMM d, yyyy')
PyQt4.QtCore.QString(u'September 16, 2013')
>>> unicode(date.toString('MMMM d, yyyy'))
u'September 16, 2013'
Viktor Kerkez
  • 45,070
  • 12
  • 104
  • 85
  • @user1871869 What are you exactly doing? `print date`? If you wan't to print it you **have** to convert it to string somehow. You can do it either by explicitly calling the `toString` method or just printing it which uses the built-in `__str__` which just prints out the type and numbers. – Viktor Kerkez Sep 17 '13 at 08:29
  • I have one question--when I was trying to print out this date, I was given an error saying: requires string as left operand, not QString Do you know how to fix this? – user1871869 Sep 17 '13 at 08:31
  • Wow, nevermind this worked perfectly. Thank you so much for your help! It also makes a lot of sense also! Thank you!!! – user1871869 Sep 17 '13 at 08:38