Format timedelta to string

后端 未结 28 1737
春和景丽
春和景丽 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 would seriously consider the Occam's Razor approach here:

    td = str(timedelta).split('.')[0]
    

    This returns a string without the microseconds

    If you want to regenerate the datetime.timedelta object, just do this:

    h,m,s = re.split(':', td)
    new_delta = datetime.timedelta(hours=int(h),minutes=int(m),seconds=int(s))
    

    2 years in, I love this language!

提交回复
热议问题