50

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?

4 Answers 4

64
import time
now = time.time()
...
later = time.time()
difference = int(later - now)
3
  • 9
    This gives an "TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.timedelta'" error in Python3. See Robert Longson's answer.
    – typoerrpr
    Jun 25, 2017 at 6:38
  • @typoerrpr This works fine for me in Python 3 as well. Apr 24, 2019 at 13:03
  • time.time() returns a float, datetime.now() returns a datetime.
    – dimo414
    Sep 26 at 20:33
53

The total_seconds method will return the difference, including any fractional part.

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

You can convert that to an integer via int() if you want

2
  • 7
    I get AttributeError: 'float' object has no attribute 'total_seconds' Jun 25, 2017 at 19:17
  • datetime.now() returns a datetime, time.time() returns a float.
    – dimo414
    Sep 26 at 20:35
8

Adding up the terms of the timedelta tuple with adequate multipliers should give you your answer. diff.days*24*60*60 + difference.seconds

from datetime import datetime
now = datetime.now()
...
later = datetime.now()
diff = later-now
diff_in_seconds = diff.days*24*60*60 + diff.seconds

The variable 'diff' is a timedelta object that is a tuple of (days, seconds, microseconds) as explained in detail here https://docs.python.org/2.4/lib/datetime-timedelta.html. All other units (hours, minutes..) are converted into this format.

>> diff = later- now
>> diff
datetime.timedelta(0, 8526, 689000)
>> diff_in_seconds = diff.days*24*60*60 + diff.seconds
>> diff_in_seconds
>> 8527

Another way to look at it would be if instead of later-now (therefore a positive time difference), you instead have a negative time difference (earlier-now), where the time elapsed between the two is still the same as in the earlier example

>> diff = earlier-now
>> diff
datetime.timedelta(-1, 77873, 311000)
>> diff_in_seconds = diff.days*24*60*60 + diff.seconds
>> diff_in_seconds
>> -8527

Hence, even if we are sure the duration is less than 1 day, it is necessary to take the day term into account, since it is an important term in case of negative time difference.

4

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.

2
  • cool, how do I create a datetime object with the floating point in seconds to print it nicely ?
    – Richard
    Sep 3, 2010 at 18:46
  • 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
    Sep 3, 2010 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.