I want to calculate the difference in minutes between two datetimes
.
My current code results in a timedelta of '1 day, 23:33:00'
.
My question is how can I convert this to minutes (or generate the timedelta in minutes)?
import datetime
import time
kodate = '2019-01-13'
kotime = '14:15'
currdatetime = datetime.datetime.now()
currdate = currdatetime.strftime("%Y-%m-%d")
currtime = currdatetime.strftime("%H:%M")
datetimeFormat = '%Y-%m-%d %H:%M'
time1 = currdate + ' ' + currtime
time2 = kodate + ' ' + kotime
timedelta = datetime.datetime.strptime(time2, datetimeFormat) - datetime.datetime.strptime(time1, datetimeFormat)
print ('Timedelta: ' + str(timedelta))
So the current timedelta
is 1 day 23 hours and 33 minutes, when I actually want 2853 (i.e. the actual number of minutes).
How do I convert Timedelta to number? You can use the following methods to convert a timedelta column to an integer column in a pandas DataFrame: Method 1: Convert Timedelta to Integer (Days) df['days'] = df['timedelta_column']. Method 2: Convert Timedelta to Integer (Hours) df['hours'] = df['timedelta_column'] / pd.
To get the Total seconds in the duration from the Timedelta object, use the timedelta. total_seconds() method.
How do I get hours from Timedelta object? Call timedelta. seconds to return the number of seconds. Use this result and the integer division symbol // to divide the seconds by 3600 to calculate the number of hours.
There's no direct method to return it in minutes, so just divide the number of seconds:
minutes = timedelta.total_seconds() / 60
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