How to get the sum of timedelta in Python?

前端 未结 6 1950
时光说笑
时光说笑 2020-12-06 09:47

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!

6条回答
  •  盖世英雄少女心
    2020-12-06 10:00

    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)
    

提交回复
热议问题