Format timedelta to string

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

    >>> str(datetime.timedelta(hours=10.56))
    10:33:36
    
    >>> td = datetime.timedelta(hours=10.505) # any timedelta object
    >>> ':'.join(str(td).split(':')[:2])
    10:30
    

    Passing the timedelta object to the str() function calls the same formatting code used if we simply type print td. Since you don't want the seconds, we can split the string by colons (3 parts) and put it back together with only the first 2 parts.

提交回复
热议问题