Format timedelta to string

后端 未结 28 1746
春和景丽
春和景丽 2020-11-22 03:57

I\'m having trouble formatting a datetime.timedelta object.

Here\'s what I\'m trying to do: I have a list of objects and one of the members of the cl

28条回答
  •  悲&欢浪女
    2020-11-22 04:23

    I know that this is an old answered question, but I use datetime.utcfromtimestamp() for this. It takes the number of seconds and returns a datetime that can be formatted like any other datetime.

    duration = datetime.utcfromtimestamp(end - begin)
    print duration.strftime('%H:%M')
    

    As long as you stay in the legal ranges for the time parts this should work, i.e. it doesn't return 1234:35 as hours are <= 23.

提交回复
热议问题