Answers for "python time difference"

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 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
Posted by: Guest on June-11-2020
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
1

get time between things python

>>> import datetime
>>> time1 = datetime.datetime.now()
>>> time2 = datetime.datetime.now() # waited a few minutes before pressing enter
>>> elapsedTime = time2 - time1
>>> elapsedTime
datetime.timedelta(0, 125, 749430)
>>> divmod(elapsedTime.total_seconds(), 60)
(2.0, 5.749430000000004) # divmod returns quotient and remainder
# 2 minutes, 5.74943 seconds
Posted by: Guest on December-01-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 "python time difference"

Python Answers by Framework

Browse Popular Code Answers by Language