Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

date

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.

like image 967
Mulagala Avatar asked Jun 14 '14 07:06

Mulagala


People also ask

How do you find the difference between two dates in hours minutes and seconds?

The easiest way to calculate the difference between two dates in Hours, Minutes, and Seconds in Data Studio, is to use DATETIME_DIFF function with the time part in the SECOND and then set the field type to Duration (sec.).

How do I get the difference between two dates and hours in Python?

Use the strptime(date_str, format) function to convert a date string into a datetime object as per the corresponding format . To get the difference between two dates, subtract date2 from date1.

How do I get the difference between two dates and minutes in Python?

timedelta() method. To find the difference between two dates in Python, one can use the timedelta class which is present in the datetime library. The timedelta class stores the difference between two datetime objects.


1 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 
like image 160
Mulagala Avatar answered Sep 27 '22 21:09

Mulagala