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)
Share:
32,791

Related videos on Youtube

user469652
Author by

user469652

Updated on August 24, 2020

Comments

  • user469652
    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
      Phong about 13 years
      ???? dont understand the question. Do you have a formula ?
    • John Machin
      John Machin about 13 years
      What 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
      orzel over 6 years
      my 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
    RobM almost 13 years
    This 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
    SilentGhost almost 13 years
    that's completely irrelevant to the question.
  • RobM
    RobM almost 13 years
    Not 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
    pelson almost 11 years
    Hmmm, 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
    Mark Byers almost 11 years
    @pelson: You are missing the second parameter. sum([datetime.timedelta(1), datetime.timedelta(0, 14700)], datetime.timedelta()) gives datetime.timedelta(1, 14700).
  • pelson
    pelson almost 11 years
    Cool. Thanks @Mark_Byers, I did miss that!
  • balleyne
    balleyne almost 11 years
    Better solution: time_sum.total_seconds() (includes seconds from the days component as well) docs.python.org/2/library/…
  • balleyne
    balleyne almost 11 years
    Better solution: myTimeDelta.total_seconds() docs.python.org/2/library/…
  • Filipe Correia
    Filipe Correia almost 11 years
    To 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
    levi about 8 years
    nice answer using reduce.
  • JamesL
    JamesL over 1 year
    python3 users will need from functools import reduce