Addition Of Two Datetime.datetime.strptime().time() Objects In Python
I want to add two time values t1 and t2 in format 'HH:MM:SS'. t1 ='12:00:00' t2='02:00:00' t1+t2 should be 14:00:00 I tried t1+t2. But as t1 & t2 are im string format the outp
Solution 1:
You can not directly add two time()
variables. This is due to the fact that these time variables are not durations. They are the time of day. You can however turn a time variable into a duration by subtracting midnight from the time variable.
Test Code:
import datetime as dt
t1 = dt.datetime.strptime('12:00:00', '%H:%M:%S')
t2 = dt.datetime.strptime('02:00:00', '%H:%M:%S')
time_zero = dt.datetime.strptime('00:00:00', '%H:%M:%S')
print((t1 - time_zero + t2).time())
Results:
14:00:00
Post a Comment for "Addition Of Two Datetime.datetime.strptime().time() Objects In Python"