0

How do I calculate the time difference between one time string say 06:05:00 and the time now in python?

Ajinkya16
  • 227
  • 2
  • 11
  • Does this answer your question? [How do I find the time difference between two datetime objects in python?](https://stackoverflow.com/questions/1345827/how-do-i-find-the-time-difference-between-two-datetime-objects-in-python) – Donnie Aug 27 '21 at 15:29
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 28 '21 at 00:50

2 Answers2

1

I did this to eliminate negative values

from datetime import datetime
now = datetime.now().time() # This is time object
s1 = '08:50:00' # This is time as string
s2 = now.strftime("%H:%M:%S") # We convert now in time format to string
HMS = '%H:%M:%S'
tdelta = datetime.strptime(s2, HMS) - datetime.strptime(s1, HMS) # We find time difference by converting them to datetime format again
print tdelta
Ajinkya16
  • 227
  • 2
  • 11
0

This is a manual solution, I suppose that through libraries it can be obtained. You can use the absolute value to avoid negatives

import datetime
now = datetime.datetime.now()
current_time = str(now.hour) +':'+ str(now.minute) +':'+ str(now.second)
another_time = '06:05:00'
splitted_current_time=[int(x) for x in current_time.split(":")]
splitted_another_time=[int(x) for x in another_time.split(":")]
diference=[];
for i in range(0,3):
    diference.append(splitted_current_time[i]-splitted_another_time[i])
print(str(diference[0])+':'+str(diference[1])+':'+str(diference[2]))