tags:

views:

51

answers:

2

I need to find the time difference in seconds with python. I know I can get the difference like this:

from datetime import datetime
now = datetime.now()
....
....
....
later = datetime.now()
difference = later-now

how do I get difference in total seconds?

+3  A: 
import time
now = time.time()
...
later = time.time()
difference = later - now
Michael Mior
+1  A: 

If all you need is to measure a time span, you may use time.time() function which returns seconds since Epoch as a floating point number.

rkhayrov
cool, how do I create a datetime object with the floating point in seconds to print it nicely ?
Richard
Are you talking about time difference or absolute time? The latter can be converted from the raw number of seconds to time struct (with year, month etc fields) with `time.localtime()` or `time.gmtime()` and then either converted to string with `time.asctime()`/``time.strftime()` or used to construct `datetime.datetime` object. I am not sure if there is any function in Python standard library to decompose/print time difference nicely (though this is much easier task than correct representation of absolute time).
rkhayrov