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.
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.).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With