Do you mean full calendar days, or groups of 24 hours?
For simply 24 hours, assuming you're using Python's datetime, then the timedelta object already has a days property:
days = (a - b).days
For calendar days, you'll need to round a down to the nearest day, and b up to the nearest day, getting rid of the partial day on either side:
roundedA = a.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
roundedB = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
days = (roundedA - roundedB).days
Referencing my comments on other answers. This is how I would work out the difference in days based on 24 hours and calender days. the days attribute works well for 24 hours and the function works best for calendar checks.
from datetime import timedelta, datetime
def cal_days_diff(a,b):
A = a.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
B = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
return (A - B).days
if __name__ == '__main__':
x = datetime(2013, 06, 18, 16, 00)
y = datetime(2013, 06, 19, 2, 00)
print (y - x).days # 0
print cal_days_diff(y, x) # 1
z = datetime(2013, 06, 20, 2, 00)
print (z - x).days # 1
print cal_days_diff(z, x) # 2
Try:
(b-a).days
I tried with b and a of type datetime.date
.
Source: Stackoverflow.com