Format timedelta to string

后端 未结 28 1742
春和景丽
春和景丽 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:11

    Following Joe's example value above, I'd use the modulus arithmetic operator, thusly:

    td = datetime.timedelta(hours=10.56)
    td_str = "%d:%d" % (td.seconds/3600, td.seconds%3600/60)
    

    Note that integer division in Python rounds down by default; if you want to be more explicit, use math.floor() or math.ceil() as appropriate.

提交回复
热议问题