Formatting Time

Thomas Guettler guettli at thomas-guettler.de
Fri Jun 3 10:45:02 EDT 2005


Am Fri, 03 Jun 2005 09:29:41 +0100 schrieb Ognjen Bezanov:

> I never thought id need help with such a thing as time formatting
> (admittadly i never did it before) but ok, i guess there is a first for
> everything.
> 
> I have a float variable representing seconds, and i want to format it
> like this:
> 
> 0:00:00  (h:mm:ss)

Hi,

Does this work? (not well tested)

i=12009 # Number of seconds
res=[]
#          d        h,       m    
for d in [24*60*60, 60*60, 60]:
    res.append("%02d" % (i/d))
    i=i%d
res.append("%02d" % i)
print ":".join(res)


# --> 00:03:20:09 (d:h:m:s)

-- 
Thomas Güttler, http://www.thomas-guettler.de/





More information about the Python-list mailing list