Converting datetime to POSIX time

Jason Baker picture Jason Baker · Oct 31, 2008 · Viewed 32.3k times · Source

How do I convert a datetime or date object into a POSIX timestamp in python? There are methods to create a datetime object out of a timestamp, but I don't seem to find any obvious ways to do the operation the opposite way.

Answer

kender picture kender · Oct 31, 2008
import time, datetime

d = datetime.datetime.now()
print time.mktime(d.timetuple())