Answers for "how to calculate time difference in python"

2

python time difference

import datetime
time_1 = datetime.strptime('05:00:00',"%H:%M:%S")
time_2 = datetime.strptime('10:00:00',"%H:%M:%S")

time_interval = time_2 - time_1
print(time_difference)
>>> 5:00:00

OR 
import time
time_1 = time.time()

time.sleep(20)

time_2 = time.time()
time_interval = time_2 - time_1
print(time_interval)
>>> 20.005916118621826

OR 
import datetime
time_1 = datetime.timedelta(hours= 10 , minutes=20, seconds=30)
time_2 = datetime.timedelta(hours= 20, minutes=30, seconds=45)
print(time_2 - time_1)
Posted by: Guest on November-16-2021
2

python difference in time

from datetime import datetime
import time

datetime1 = datetime.datetime.now()
# Wait for 1 minute
time.sleep(60) 
datetime2 = datetime.datetime.now()

difference = datetime2 - datetime1
print(f"The time difference between the 2 time is: {difference}")
Posted by: Guest on September-18-2020
0

python datetime difference in seconds

import datetime as dt

a = dt.datetime(2013,12,30,23,59,59)
b = dt.datetime(2013,12,31,23,59,59)

(b-a).total_seconds()
Posted by: Guest on June-09-2020

Code answers related to "how to calculate time difference in python"

Python Answers by Framework

Browse Popular Code Answers by Language