How do I sum timedeltas in a loop? If I add the timedeltas manually the sum is correct. But if I use a while loop to sum the timedeltas I get this error:
TypeError: can only concatenate str (not "datetime.timedelta") to str
And the other question is, if the sum value is over 24 hours- python prints sum as one day and x hours. I need the sum value for example in 26:15 (26hours and 15 minutes).
import datetime
fmt = '%H%M'
start = [];end = [];td = []
start.append( datetime.datetime.strptime('0300', fmt) )
start.append( datetime.datetime.strptime('0645', fmt) )
end.append( datetime.datetime.strptime('1505', fmt) )
end.append( datetime.datetime.strptime('1845', fmt) )
td.append( end[0] - start[0] )
td.append( end[1] - start[1] )
print('timedelta1= ' + str(td[0]))
print('timedelta2= ' + str(td[1]))
tSum = td[0] +td[1]
print( 'Sum: ' + str(tSum) )
tSumLoop = ''
i = 0
while i < 2:
tSumLoop += td[i]
i += 1
print(tSumLoop)
How do I solve these two problems? Thanks for your help!