Python gets the time in minutes

Python gets the time in minutes … here is a solution to the problem.

Python gets the 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,

Solution

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 a third term using diff.microseconds and convert to minutes with the appropriate divisor.) )

And after the release of Python 2.7 you can use methods total_seconds

print (diff.total_seconds() / 60)

Related Problems and Solutions