Time difference in seconds (as a floating point)
52,568
Solution 1
combined = delta.seconds + delta.microseconds/1E6
Solution 2
for newer version of Python (Python 2.7+ or Python 3+), you can also use the method total_seconds
:
from datetime import datetime
t1 = datetime.now()
t2 = datetime.now()
delta = t2 - t1
print(delta.total_seconds())
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)
Author by
pocoa
Updated on December 18, 2020Comments
-
pocoa over 3 years
>>> from datetime import datetime >>> t1 = datetime.now() >>> t2 = datetime.now() >>> delta = t2 - t1 >>> delta.seconds 7 >>> delta.microseconds 631000
Is there any way to get that as 7.631000 ? I can use time module, but I also need that t1 and t2 variables as DateTime objects. So if there is an easy way to do it with datettime, that would be great. Otherwise it'll look ugly:
t1 = datetime.now() _t1 = time.time() t2 = datetime.now() diff = time.time() - _t1
-
pocoa almost 14 yearsor combined = delta.seconds + (float(1) / delta.microseconds)
-
underrun over 7 years@pocoa - this is actually an incorrect conversion. 1/time is a rate (Hz) which really doesn't make sense here. this also clearly provides a different result than the accepted answer on which you commented.