python - Difference between two unix timestamps

Viewed 20343

I have two timestamps in miliseconds and i want to compute the difference between the two in minutes:

d1 = 1502053449617 

current_time_utc = int(round(time.time() * 1000))

The values for d1 are dynamically generated by a third party API and are in UTC . I am trying to get the difference between the current time in UTC and d1.

fmt = '%Y-%m-%d %H:%M:%S'

 time1  = datetime.strptime(d1, fmt)
 time2  = datetime.strptime(current_time_utc, fmt)

I want to be able to find the difference between the two (time1 - time2) . If i do the below , i get an error saying "string expected, long given"

print(    time1-time2)

I want the difference between the two in minutes . Please help

2 Answers

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
Related