python - Difference between two unix timestamps

You don't need to format the string, you just need to convert the timestamp directly, by first dividing it by 1000. Then its just a matter of printing out the differences (and calculating it in minutes):

from __future__ import division
import datetime

d1 = 1502053449617

converted_d1 = datetime.datetime.fromtimestamp(round(d1 / 1000))
current_time_utc = datetime.datetime.utcnow()

print((current_time_utc - converted_d1))
print((current_time_utc - converted_d1).total_seconds() / 60)

The above prints:

3 days, 5:08:14.087515
4628.234791916667

I had to calculate the difference between two unix timestamps - but in days, as follows:

create two unix timestamps:

import datetime

timestamp1 = datetime.datetime(2017, 12, 1).strftime('%s')
timestamp2 = datetime.datetime(2017, 11, 14).strftime('%s')

print(timestamp1)
print(timestamp2)

1512079200
1510610400

calculate the day difference:

print((float(timestamp1)-float(timestamp2))/(60*60*24))

output:

17.0

Tags:

Python 2.7