James Morris - JMOZ

Webmaster in '97.

Python: Convert Datetime to Timestamp and Back Again

| Comments

It’s pretty easy to get a datetime object from a timestamp in Python. However the same can not be said the other way around. Just when I start to think “oh Python’s pretty cool with a nice API” the further you look into the language and library you start to notice some cracks.

Convert a timestamp to a datetime object

1
2
3
4
5
from datetime import datetime

print datetime.fromtimestamp(1346236702)

#2012-08-29 11:38:22

Convert a datetime object to a timestamp

Why oh why did they not just implement a datetime.totimestamp() method on datetime?

1
2
3
4
5
6
7
8
from datetime import datetime
import time

dt = datetime.fromtimestamp(1346236702)

print time.mktime(dt.timetuple())

#1346236702.0

Comments