简体   繁体   中英

python get time in minutes

import datetime
start = datetime.datetime(2009, 1, 31)
end = datetime.datetime(2009, 2, 1)
print end-start
>>1 day, 0:00:00//output

How to get the output in minutes

Thanks,

import datetime
start = datetime.datetime(2009, 1, 31)
end = datetime.datetime(2009, 2, 1)
diff = end-start
print (diff.days * 1440) + (diff.seconds / 60)
>> 1440.0

(I'm assuming you don't need microsecond resolution here - but if you do, just add in a third term using diff.microseconds with the proper divisor to convert to minutes.)

and after the release of the python 2.7 you can use the method total_seconds

print (diff.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