Python: Converting from `datetime.datetime` to `time.time` Python: Converting from `datetime.datetime` to `time.time` python python

Python: Converting from `datetime.datetime` to `time.time`


It's not hard to use the time tuple method and still retain the microseconds:

>>> t = datetime.datetime.now()>>> tdatetime.datetime(2011, 11, 5, 11, 26, 15, 37496)>>> time.mktime(t.timetuple()) + t.microsecond / 1E61320517575.037496


time.mktime(dt_obj.timetuple())

Should do the trick.


I know this is an old question, but in python 3.3+ there is now an easier way to do this using the datetime.timestamp() method:

from datetime import datetimetimestamp = datetime.now().timestamp()