Mulagala Mulagala - 1 year ago 175
Python Question

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

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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download