python - Subtract dates in Django? -
i have 2 datefield variables , subtract them , return difference number of months nearest month. how might this? thanks help! for definite answer using calendar month lengths: months = lambda a, b: abs((a.year - b.year) * 12 + a.month - b.month) example: >>> import datetime >>> = datetime.date(2011, 2, 8) >>> b = datetime.date(2010, 5, 14) >>> months(a, b) 9 edit, if want round based on days too: months = lambda a, b: abs((a.year - b.year) * 12 + a.month - b.month) + int(abs(a.day - b.day) > 15) example: >>> import datetime >>> = datetime.date(2011, 2, 8) >>> b = datetime.date(2010, 5, 14) >>> months(a, b) 9 >>> b = datetime.date(2010, 5, 30) >>> months(a, b) 10