time conversion

flupke flupke at nonexistingdomain.com
Thu Oct 27 11:02:22 EDT 2005


flupke wrote:
> Is there an easy to convert following hour notation hh:mm
> to decimals?
> For instance 2 hours and 30 minutes as 2:30 to 2,50
> I don't really know where to search for this kind of conversion.
> 
> Thanks,
> Benedict

I found a way. Not sure if it's the best:

time = "2:3"
factor = 100.0/60.0

time_parts = time.split(":")
minutes = int(float(time_parts[1]) * factor)
if ( len(str(minutes)) == 1 ): minutes *= 10 # to get 50 instead of 5
     time_new = "%s,%.02s" % (time_parts[0],minutes)
     print "%s -> %s " % (time,time_new)



More information about the Python-list mailing list