How to convert a timedelta to minutes?

Rosco picture Rosco · Jan 11, 2019 · Viewed 8.6k times · Source

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

Answer

Jonah Bishop picture Jonah Bishop · Jan 11, 2019

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

minutes = timedelta.total_seconds() / 60