How do I check the difference, in seconds, between two dates?


Question

There has to be an easier way to do this. I have objects that want to be refreshed every so often, so I want to record when they were created, check against the current timestamp, and refresh as necessary.

datetime.datetime has proven to be difficult, and I don't want to dive into the ctime library. Is there anything easier for this sort of thing?

1
147
6/24/2013 8:01:46 PM

Accepted Answer

if you want to compute differences between two known dates, use total_seconds like this:

import datetime as dt

a = dt.datetime(2013,12,30,23,59,59)
b = dt.datetime(2013,12,31,23,59,59)

(b-a).total_seconds()

86400.0

#note that seconds doesn't give you what you want:
(b-a).seconds

0

428
6/19/2013 12:35:26 PM

import time  
current = time.time()

...job...
end = time.time()
diff = end - current

would that work for you?


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon