Skip to content Skip to sidebar Skip to footer

How To Convert Float Into Hours Minutes Seconds?

I've values in float and I am trying to convert them into Hours:Min:Seconds but I've failed. I've followed the following post: Converting a float to hh:mm format For example I've g

Solution 1:

You can make use of the datetime module:

import datetime
time = 0.6
result = str(datetime.timedelta(minutes=time))

Solution 2:

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

Solution 3:

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)

Post a Comment for "How To Convert Float Into Hours Minutes Seconds?"