简体   繁体   中英

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 (ie the actual number of minutes).

没有直接的方法可以在几分钟内返回它,因此只需将秒数除以即可:

minutes = timedelta.total_seconds() / 60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM