Partially Attended

an irregularly updated blog by Ian Mulvany

Converting between dates and unix time in Python

Thu Oct 13, 2011

49 Words

Going from a date to a unix time:

>>> from datetime import date
>>> from time import mktime
>>> start = date(2011, 9, 26)
>>> mktime(start.timetuple())
1316991600.0

Going from a unix time to a date:

>>> from time import strftime
>>> from datetime import datetime
>>> datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d %H:%M:%S')
'2010-09-10 07:51:25'
This work is licensed under a Creative Commons Attribution 4.0 International License