12

I want to print a float value that I've got after performing some math as time string in the hh:mm format. Currently I have the float like 9.888888888888886 and I want it in time like 09:50. I have tried the following code:

    time = str(time)
    time = time.split(".")
    time[1] = float(time[1])
    time[1] *= 0.6
    time[1] = str(time[1])

and when I print I use

    str(time[0]) + ":" + time[1][:2]

Any way to achieve this effect consistently? With more advanced inputs my above code does not work properly, and outputs the wrong time.

ZF007
  • 3,708
  • 8
  • 29
  • 48
Liam Rahav
  • 275
  • 1
  • 5
  • 17

5 Answers5

18

Alternatively to using datetime, you can just use simple formatting with a bit of maths, eg:

result = '{0:02.0f}:{1:02.0f}'.format(*divmod(time * 60, 60))

(This assumes that time is nonnegative.)

Community
  • 1
  • 1
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
15

Using datetime.timedelta:

>>> import datetime
>>> datetime.timedelta(hours=9.888888888888886)
datetime.timedelta(0, 35600)
>>> str(datetime.timedelta(hours=9.888888888888886))
'9:53:20'
>>> str(datetime.timedelta(hours=9.888888888888886)).rsplit(':', 1)[0]
'9:53'

>>> datetime.datetime.combine(datetime.date.today(), datetime.time()) + \
... datetime.timedelta(hours=9.888888888888886)
datetime.datetime(2014, 12, 16, 9, 53, 20)
>>> _.strftime('%H:%M')
'09:53'
falsetru
  • 357,413
  • 63
  • 732
  • 636
9

Using divmod you get the remainder and the quotient of a division.

minutes = 9.888888888888886*60
hours, minutes = divmod(minutes, 60)

print "%02d:%02d"%(hours,minutes)

Output:

09:53

If you want the seconds too:

seconds = 9.888888888888886*60*60
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)

print "%02d:%02d:%02d"%(hours,minutes,seconds)
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
3

For python 3.7 version this becomes an one-liner.

x = 9.888888888888886

print (str(datetime.timedelta(hours=x))[:-3])

(Here the time-string minus the last three chars is printed.)

Result 1 : 9:53

Alternatively if you needs seconds:

print (datetime.timedelta(hours=x))

Result 2 : 9:53:20

And finally if you go beyond the 24hour mark timedelta shows the added day as well:

x = 39.888888888888886

Result 3 : 1 day, 15:53:20

ZF007
  • 3,708
  • 8
  • 29
  • 48
1

Assuming you want to turn 9.888888888888886 into 09:53, based on the former being floating point hours, and you don't want to worry about days, without a library you could simply do:

fhours = 9.888888888888886
ihours = int(ihours)
"%02d:%02d" % (ihours,(fhours-ihours)*60)

With only the math module, try

import math
"%02d:%02d" % (fhours,math.modf(fhours)[0]*60)

You might think that using the datetime module would help, but it's actually a bit fiddly. You can get a timedelta object out by passing in your fractional hours:

 import datetime
 td = datetime.timedelta(hours=9.888888888888886)

But getting that into a time object or a datetime object to format is the tricky bit, you end up having to do something like

 import datetime
 td = datetime.timedelta(hours=9.888888888888886)
 (datetime.datetime(2000,1,1)+td).strftime("%H:%M")

(That uses a randomly picked date to get the time delta into a datetime to use)

Gagravarr
  • 47,320
  • 10
  • 111
  • 156