I have a timestamp 1353954367108 . I'd like to parse this to a datetime object in Python. How can I do that?
Asked
Active
Viewed 1,992 times
2
-
If Python doesn't have call that specifically handles that, divide by 1000! – ikegami Jun 07 '13 at 16:21
-
Under the assumption the timestamp is expressed as _ms_ not _s_, this is a duplicate ... but the question is more understandable than the original ;) – Sylvain Leroux Jun 07 '13 at 16:58
1 Answers
3
This is indeed a duplicate if we assume your timestamp as expressed in 1/1000s (Unix timestamps are usually in seconds) In that case, the answer is datetime.fromtimestamp
:
>>> from datetime import datetime
>>> datetime.fromtimestamp(1353954367108/1000.)
# don't forget the dot ^ to force floating point division!
datetime.datetime(2012, 11, 26, 19, 26, 7)
If your timestamp is really in seconds, well you have to find an other solution able to deal with dates in distant future!

Sylvain Leroux
- 50,096
- 7
- 103
- 125
-
to allow easy round-trip: `utc_dt = datetime.utcfromtimestamp(millis // 1000).replace(microsecond=1000 * (millis % 1000))` – jfs Jun 07 '13 at 19:16