python datetime calculate time difference code example
Example 1: python difference in time
from datetime import datetime
import time
datetime1 = datetime.datetime.now()
time.sleep(60)
datetime2 = datetime.datetime.now()
difference = datetime2 - datetime1
print(f"The time difference between the 2 time is: {difference}")
Example 2: 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()
Example 3: time difference between two datetime.time
dateTimeA = datetime.datetime.combine(datetime.date.today(), a)
dateTimeB = datetime.datetime.combine(datetime.date.today(), b)
dateTimeDifference = dateTimeA - dateTimeB
dateTimeDifferenceInHours = dateTimeDifference.total_seconds() / 3600