Stiley Stiley - 4 months ago 22
Python Question

Python - difference between time

Looking for easiest way to calculate the difference between 2 python times and display the millisecond delta

I have 2 times

startTime = datetime.datetime.now().time()


do some stuff...

endTime= datetime.datetime.now().time()


This works fine and when I log the times out and I get something like this in my logs...

RequestStartTime = 08:56:19.188999

ResponseTime = 08:56:19.905999


When I try to simply subtract them like this

delta = endTime - startTime


I get the following error

unsupported operand type(s) for -: 'time' and 'time'


All I want to do is show the difference in microseconds and I can't figure it out

I want to show is
717000 ms

Answer Source

If you just use the result of now(), and don't convert them to times, you can take the difference & extract the bits you want in the form you want; for example:

startTime = datetime.datetime.now()
endTime= datetime.datetime.now()
delta = endTime - startTime
print str(delta).split(":")[2]