How can I format timedelta for display

后端 未结 6 1932
Happy的楠姐
Happy的楠姐 2020-12-20 13:13

My script calculate the difference in 2 time. Like this:

lasted = datetime.strptime(previous_time, FMT) - datetime.strptime(current_time, FMT)
6条回答
  •  眼角桃花
    2020-12-20 13:54

    That fractional second bit is sometimes unwanted from a timedelta. A quick truncate of that fractional bit with a split and discard:

    a = datetime.now()
    b = datetime.now() - a
    

    then

    str(b).split('.')[0]
    

    (assuming applications where fraction of a second is irrelevant to you)

提交回复
热议问题