How can you convert QDate in PyQt5 to a datetime.date?

user1921942 picture user1921942 · Mar 17, 2014 · Viewed 11.2k times · Source

I am trying to convert a date gathered from a QDateEdit into datetime.date for use in a sqlite3 database. I have seen that in PyQt4 you could use toPyDateTime however it doesnt seem to be in PyQt5. If this function is completely gone is there a way to convert QDate into a string?

Answer

ekhumoro picture ekhumoro · Mar 17, 2014

What version of PyQt5 are you using?

>>> from PyQt5.QtCore import QDate, QDateTime
>>> QDate.currentDate().toPyDate()
datetime.date(2014, 3, 17)
>>> QDateTime.currentDateTime().toPyDateTime()
datetime.datetime(2014, 3, 17, 19, 9, 45, 974000)

This is using Qt-5.2.1 with PyQt-5.2 or PyQt-5.1.1.