How to convert float into Hours Minutes Seconds?

强颜欢笑 提交于 2019-12-02 08:33:47

Divmod function accepts only two parameter hence you get either of the two Divmod()

So you can try doing this:

time = 0.6
mon, sec = divmod(time, 60)
hr, mon = divmod(mon, 60)
print "%d:%02d:%02d" % (hr, mon, sec)

You're not obtaining the hours from anywhere so you'll first need to extract the hours, i.e.:

float_time = 0.6  # in minutes
hours, seconds = divmod(float_time * 60, 3600)  # split to hours and seconds
minutes, seconds = divmod(seconds, 60)  # split the seconds to minutes and seconds

Then you can deal with formatting, i.e.:

result = "{:02.0f}:{:02.0f}:{:02.0f}".format(hours, minutes, seconds)
# 00:00:36

You can make use of the datetime module:

import datetime
time = 0.6
result = str(datetime.timedelta(minutes=time))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!