Chenna
Chenna

Reputation: 62

Comparing two times in Hours using python

I've two dates with time zone formatted as ('%Y-%m-%dT%H:%M:%SZ')

Given_Time = '2020-02-12T02:12:12Z'
current_time = '2020-02-11T06:22:42Z' 

I wanted to compare these two times in Hrs. I mean given date is how many hours behind the current time

Upvotes: 0

Views: 52

Answers (1)

Onyambu
Onyambu

Reputation: 79208

import datetime
def time_diff(x, y):
     x = datetime.datetime.strptime(x,'%Y-%m-%dT%H:%M:%SZ')
     y = datetime.datetime.strptime(y,'%Y-%m-%dT%H:%M:%SZ')
     return (x-y).total_seconds()/3600

time_diff(Given_Time, current_time)
19.825
time_diff(current_time, Given_Time)
-19.825

Upvotes: 3

Related Questions