Skip to content Skip to sidebar Skip to footer

Time Difference In Seconds (as A Floating Point)

>>> from datetime import datetime >>> t1 = datetime.now() >>> t2 = datetime.now() >>> delta = t2 - t1 >>> delta.seconds 7 >>>

Solution 1:

for newer version of Python (Python 2.7+ or Python 3+), you can also use the method total_seconds:

from datetime importdatetimet1= datetime.now()
t2 = datetime.now()
delta = t2 - t1
print(delta.total_seconds())

Solution 2:

combined = delta.seconds + delta.microseconds/1E6

Solution 3:

I don't know if there is a better way, but:

((1000000 * delta.seconds + delta.microseconds) / 1000000.0)

or possibly:

"%d.%06d"%(delta.seconds,delta.microseconds)

Post a Comment for "Time Difference In Seconds (as A Floating Point)"