How to get the difference between two dates in hours, minutes and seconds.?

Question:

I have taken two dates as

import datetime
data1 = datetime.datetime.now()
data2 = datetime.datetime.now()

I have done it like this, I’m getting minutes and seconds. I want hours too; how can I do this?

My code:

diff = data2-data1
divmod(diff.days * 86400 + diff.seconds, 60)
(3, 45)

How can I get hours too? Any help would be appreciated.

Asked By: Mulagala

||

Answers:

Finally found solution

import datetime
data1 = datetime.datetime.now()
data2 = datetime.datetime.now()

diff = data2 - data1

days, seconds = diff.days, diff.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60

print hours,minutes,seconds
Answered By: Mulagala
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.