Question

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,

Was it helpful?

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 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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top