Take the 2-minute tour ×

I'd like to convert an epoch time stamp (eg, et =1351036517.179364) to a datetime.datetime object. So far, I've used time.ctime(et) which gives me a string of something like "Fri Oct 5 22:20:33 2012 ".

Eventually, I need the datetime object to compute time difference between two datapoints, the other datapoint is a datetime object as well.


share|improve this question
timestamps can be directly substracted to give you the time diff in seconds. –  Marc B Nov 13 '12 at 19:04

2 Answers 2

up vote 2 down vote accepted

It's worthwhile to remember that a timestamp has no associated timezone information. Unix time is unambiguously UTC, but it's not unusual to store local time in Unix-style timestamps. You must check against a known reference to be sure.

If your timestamps are already in UTC (Unix time), you can use Marc B's suggestion of subtract two unix timestamps directly to give you the number of seconds of difference between them. You may then want to create a timedelta, which will allow you to easily manipulate datetimes with it (you can sum or subtract a timedelta to/from a datetime, for example).

datetime.timedelta( seconds= n_seconds )

If your timestamps are in local time, don't subtract them directly, as you'll get potentially incorrect results.

Instead, you should use datetime.fromtimestamp first, attach a timezone to each datetime, and only then subtract them to get the timedelta.

To convert back from a timedelta to the number of seconds, there's timedelta.total_seconds, assuming you're using python 2.7 . If you have an earlier version, you'll have to do the calculation by hand, or pip install datetime

share|improve this answer
Got it. Thanks so much @goncalopp and Marc B! –  msunbot Nov 13 '12 at 21:22
it is wrong. There is no such thing "unix timestamp in local time". 1351036517.179364 unix timestamp corresponds to 2012-10-24T01:55:17.179363 in Paris, 2012-10-23T19:55:17.179363 in New York. You can substruct timestamps directly -- they are "seconds since Epoch" (elapsed seconds if we ignore the difference between SI seconds and mean solart (UT1) seconds. Ignoring leap seconds in other words). –  J.F. Sebastian Dec 2 '14 at 15:06
@J.F.Sebastian Technically, you're right, because such is the definition of "unix time". However, it's certainly not unheard of to encode timestamps obtained from local time as if it were UTC (time.mktime(datetime.now().timetuple()) vs time.mktime(datetime.utcnow().timetuple())). Of course, strictly, these are not unix time timestamps. I'll edit the answer to make this clearer –  goncalopp Dec 2 '14 at 17:30
don't confuse people. OP asks about "epoch timestamp" meaning POSIX timestamp (POSIX time). –  J.F. Sebastian Dec 3 '14 at 7:37
@J.F.Sebastian You'll also notice that OP posted an example timestamp and seemingly automatically assumes it's Unix time. Personally, having had to parse timestamps in localtime before while assuming they were Unix time, I'd much rather that someone had pointed this out to me than insisted that everything that people informally call "Unix time" is in UTC. IMMV. –  goncalopp Dec 3 '14 at 13:15

To convert POSIX timestamp to datetime.datetime object that represents time in UTC:

from datetime import datetime, timedelta, timezone

timestamp = 1351036517.179364
utc_time = datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=timestamp)
# -> 2012-10-23 23:55:17.179363+00:00

If your Python version does not support datetime.timezone; you could omit it, to get a naive datetime object in UTC.


If naive datetime objects represent local times that have different UTC offsets than you can't compare them directly otherwise you may get a wrong result. You either need timezone-aware datetime objects or you should convert the datetime objects to UTC first. See

share|improve this answer

Your Answer


