Convert Datetime To Unix Timestamp And Convert It Back In Python
Answer : solution is import time import datetime d = datetime.date(2015,1,5) unixtime = time.mktime(d.timetuple()) What you missed here is timezones. Presumably you've five hours off UTC, so 2013-09-01T11:00:00 local and 2013-09-01T06:00:00Z are the same time. You need to read the top of the datetime docs, which explain about timezones and "naive" and "aware" objects. If your original naive datetime was UTC, the way to recover it is to use utcfromtimestamp instead of fromtimestamp . On the other hand, if your original naive datetime was local, you shouldn't have subtracted a UTC timestamp from it in the first place; use datetime.fromtimestamp(0) instead. Or, if you had an aware datetime object, you need to either use a local (aware) epoch on both sides, or explicitly convert to and from UTC. If you have, or can upgrade to, Python 3.3 or later, you can avoid all of these problems by just using the timestamp method instead of trying to figure out how to