irom
irom

Reputation: 3596

getting wrong time delta in python

I am subtracting two dates in python 2.7 and getting wrong result in seconds. Apparently difference between these dates is more than 24h which is 86400s. But I am getting 44705s, why is that and how to fix it ?

>>> date1
datetime.datetime(2017, 10, 22, 11, 41, 28)
>>> date2
datetime.datetime(2017, 10, 20, 23, 16, 23)
>>> (date1-date2).seconds
44705

Upvotes: 1

Views: 707

Answers (2)

cs95
cs95

Reputation: 402493

Calling .seconds will only give you the seconds component of the timedelta object, which only takes into account seconds, minutes, and hours (see docs for more detail). If you want the entire timedelta in seconds, call total_seconds.

>>> (date1 - date2).total_seconds()  
131105.0

Upvotes: 4

John Zwinck
John Zwinck

Reputation: 249153

date1-date2 is datetime.timedelta(1, 44705). You're only looking at the seconds portion. Look at the days portion too.

Upvotes: 1

Related Questions