0

I am trying to calculate the difference between two different time stamps mentioned below. I am getting a value of 25324 seconds which is less than the actual difference . What is the issue here ?

from datetime import datetime

time_format = "%Y-%m-%d %H:%M:%S"

d1 = datetime.strptime('2013-12-12 03:59:33', time_format)
d2 = datetime.strptime('2013-12-09 20:57:29', time_format)
print (d1 - d2).seconds

25324
0

1 Answer 1

5

A timedelta object contains the time in days, seconds and microseconds. You have to add them together to get the full number.

from datetime import datetime

time_format = "%Y-%m-%d %H:%M:%S"

d1 = datetime.strptime('2013-12-12 03:59:33', time_format)
d2 = datetime.strptime('2013-12-09 20:57:29', time_format)

diff  = d1 - d2
print diff  # 2 days, 7:02:04
print diff.days * 24 * 60 * 60 + diff.seconds  # 198124

The method total_seconds() does that automatically.

The point is you can't store a number that might need accuracy over the range from 999999999 days to 1 microsecond simultaneously in a single float, so it's split into three integers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.