user105939
user105939

Reputation: 219

How to add two timedate variables?

total = "00:00:00"
# calculating a "lap" , it will never have Y,M,D , but the log contain it.
laptime = str(datetime.strptime(timeend, "%Y-%m-%d %H:%M:%S") - datetime.strptime(timestart, "%Y-%m-%d %H:%M:%S"))

#failing at hiw to add it to "total"
total = datetime.combine(datetime.strptime(total, "%H:%M:%S") + datetime.strptime(laptime, "%H:%M:%S"))

I tried combine but no success. how can I add(accumulate) laptime ?

Upvotes: 2

Views: 36

Answers (1)

Masum billah
Masum billah

Reputation: 982

try this

import datetime as datetime
t1 = datetime.strptime("2018-02-01 14:55:27", "%Y-%m-%d %H:%M:%S")
t2 = datetime.strptime("2018-02-01 14:59:24", "%Y-%m-%d %H:%M:%S")

#for difference
r1 = str(t2 - t1) 
#output 
'0:03:57'

#for adding
r2 = str(t2+ (t2-t1))

#output 
'2018-02-01 15:03:21'

Upvotes: 2

Related Questions