I am trying to find the difference between two converted time strings from my dictionary. I have converted the strings from "historical values" into datetime objects using strptime() but when I deduct one from another I get a TypeError.
from datetime import datetime
low_end_value = 50.00
stockDict = {'Historic value': [('16:00:00:00', 55.50), ("15:00:00:00", 45.50),
("14:00:00:00", 75.50), ("13:00:00:00", 65.50), ("12:00:00:00", 55.50)]}
x = 0
while (stockDict['Historic value'][x][1]) > low_end_value:
if (stockDict['Historic value'][x][1]) < low_end_value:
break
x += 1
date_str1 = (stockDict['Historic value'][(x - 1)][0])
# (n-1)th historical time
date_str2 = (stockDict['Historic value'][x][0])
# (n)th historical time
associated_time1 = (datetime.strptime(date_str1, '%H:%M:%S:%f').time())
# converted to a datetime object
associated_time2 = (datetime.strptime(date_str2, '%H:%M:%S:%f').time())
# converted to a datetime object
difference_in_time = associated_time1 - associated_time2
print(difference_in_time)
the output I receive is:
Traceback (most recent call last):
File "C:/Users/engli/PycharmProjects/Calculated time.py", line 47, in <module>
difference_in_time = associated_time1 - associated_time2
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
kindest regards,
Andrew
Try using datetime.combine() method.
from datetime import datetime, date
datetime.combine(date.min, associated_time1) - datetime.combine(date.min, associated_time2)