How to get the sum of timedelta in Python?
32,791
Solution 1
datetime combine method allows you to combine time with a delta
datetime.combine(date.today(), time()) + timedelta(hours=2)
timedelta can be combined using usual '+' operator
>>> timedelta(hours=3)
datetime.timedelta(0, 10800)
>>> timedelta(hours=2)
datetime.timedelta(0, 7200)
>>>
>>> timedelta(hours=3) + timedelta(hours=2)
datetime.timedelta(0, 18000)
>>>
You can read the datetime module docs and a very good simple introduction at
Solution 2
To add timedeltas you can use the builtin operator +
:
result = timedelta1 + timedelta2
To add a lot of timedeltas you can use sum:
result = sum(timedeltas, datetime.timedelta())
Or reduce:
import operator
result = reduce(operator.add, timedeltas)
Related videos on Youtube
Author by
user469652
Updated on August 24, 2020Comments
-
user469652 over 3 years
Python: How to get the sum of timedelta?
Eg. I just got a lot of timedelta object, and now I want the sum. That's it!
-
Phong about 13 years???? dont understand the question. Do you have a formula ?
-
John Machin about 13 yearsWhat do you do if you have a lot of integer objects, and you want the sum? What have you tried? What happened when you tried?
-
orzel over 6 yearsmy guess is that if you have tds = [ timedelta(), timedelta(), timedelta(), ...], he wants to know how to do sum(tds). This is not possible as is, as sum kind starts with 0 and hence try to do 0+timedelta(). You can of course do it with some basic loop duration = timedelta(0); for td in tds: duration += td
-
-
RobM almost 13 yearsThis doesn't work for timedeltas with a
days
component:datetime.timedelta(days=3, hours=5, seconds=10).seconds -> 18010
. It should be 277210s ((3*24*60*60) + (5*60*60) + 10). -
SilentGhost almost 13 yearsthat's completely irrelevant to the question.
-
RobM almost 13 yearsNot necessarily. I share Goladus's interpretation - that user469652 wants to sum the components of a timedelta into a single primitive type, such as seconds. That's certainly what I was looking for when I found this question.
-
pelson almost 11 yearsHmmm, I would expect the sum function to work too, but it appears not for me on 2.7.3. The following fails:
sum([datetime.timedelta(1), datetime.timedelta(0, 14700)])
. -
Mark Byers almost 11 years@pelson: You are missing the second parameter.
sum([datetime.timedelta(1), datetime.timedelta(0, 14700)], datetime.timedelta())
givesdatetime.timedelta(1, 14700)
. -
pelson almost 11 yearsCool. Thanks @Mark_Byers, I did miss that!
-
balleyne almost 11 yearsBetter solution:
time_sum.total_seconds()
(includes seconds from the days component as well) docs.python.org/2/library/… -
balleyne almost 11 yearsBetter solution:
myTimeDelta.total_seconds()
docs.python.org/2/library/… -
Filipe Correia almost 11 yearsTo guide others that might be searching for this, the error you get without the second parameter is
TypeError: unsupported operand type(s) for +: 'type' and 'datetime.timedelta'
. And the official docs that explain why this second argument is needed are here. -
levi about 8 yearsnice answer using reduce.
-
JamesL over 1 yearpython3 users will need
from functools import reduce