Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a timedelta to minutes?

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).

like image 696
Rosco Avatar asked Jan 11 '19 14:01

Rosco


People also ask

How do I convert Timedelta?

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.

How do you convert Timedelta to seconds?

To get the Total seconds in the duration from the Timedelta object, use the timedelta. total_seconds() method.

How do you get Timedelta in hours?

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.


1 Answers

There's no direct method to return it in minutes, so just divide the number of seconds:

minutes = timedelta.total_seconds() / 60
like image 90
Jonah Bishop Avatar answered Oct 05 '22 23:10

Jonah Bishop