12

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

martineau
  • 119,623
  • 25
  • 170
  • 301
Rosco
  • 121
  • 1
  • 1
  • 4

1 Answers1

23

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

minutes = timedelta.total_seconds() / 60
Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74