Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting QdateTime to normal python dateTime?

I have a lot of existing code that just uses the normal dateTime class in python, however in upgrading my program I am using the QtGui.QdateTimeEdit() class, but that class returns a QdateTime object that seems to be incompatible with the normal dateTime object.

So, is there a sane way to convert QdateTime to normal python dateTime? Other then breaking it into its parts and recreating a normal dateTime object from that? I am using PyQt4 with Python 3.2. Thanks.

like image 202
Scott C Avatar asked Dec 12 '11 00:12

Scott C


People also ask

How do you convert an object to a date in python?

The date column is indeed a string, which—remember—is denoted as an object type in Python. You can convert it to the datetime type with the . to_datetime() method in pandas .

How do I convert a datetime to a number in Python?

strftime() object. In this method, we are using strftime() function of datetime class which converts it into the string which can be converted to an integer using the int() function. Returns : It returns the string representation of the date or time object.

How do I convert a datetime to a string in Python?

The strftime() method returns a string representing date and time using date, time or datetime object.


1 Answers

QDateTime has a toPyDateTime method which will return regular datetime objects.

In : from PyQt4 import QtCore

In : QtCore.PYQT_VERSION_STR
Out: '4.8.6'

In : QtCore.QT_VERSION_STR
Out: '4.7.4'

In : now = QtCore.QDateTime.currentDateTime()

In : now
Out: PyQt4.QtCore.QDateTime(2011, 12, 11, 20, 12, 47, 55)

In : now.toPyDateTime()
Out: datetime.datetime(2011, 12, 11, 20, 12, 47, 55000)
like image 61
Avaris Avatar answered Sep 23 '22 05:09

Avaris