python time difference code example
Example 1: python print time difference
>>> import datetime
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
>>> c = b - a
>>> c
datetime.timedelta(0, 4, 316543)
>>> c.days
0
>>> c.seconds
4
>>> c.microseconds
316543
Example 2: 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 3: get time between things python
>>> import datetime
>>> time1 = datetime.datetime.now()
>>> time2 = datetime.datetime.now()
>>> elapsedTime = time2 - time1
>>> elapsedTime
datetime.timedelta(0, 125, 749430)
>>> divmod(elapsedTime.total_seconds(), 60)
(2.0, 5.749430000000004)
Example 4: 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()