How do I convert seconds to hours, minutes and seconds?

前端 未结 12 2286
执笔经年
执笔经年 2020-11-22 11:00

I have a function that returns information in seconds, but I need to store that information in hours:minutes:seconds.

Is there an easy way to convert the seconds to

12条回答
  •  不要未来只要你来
    2020-11-22 11:31

    dateutil.relativedelta is convenient if you need to access hours, minutes and seconds as floats as well. datetime.timedelta does not provide a similar interface.

    from dateutil.relativedelta import relativedelta
    rt = relativedelta(seconds=5440)
    print(rt.seconds)
    print('{:02d}:{:02d}:{:02d}'.format(
        int(rt.hours), int(rt.minutes), int(rt.seconds)))
    

    Prints

    40.0
    01:30:40
    

提交回复
热议问题