If you do not need a day for a month, you can do the following:
date1.year * 12 + date1.month - (date2.year * 12 + date2.month)
Dates for the same month, for example. '2016-12-31' and '2016-12-1' will give you 0.
, , . . 30 , 0/30, 29/30.
:
from calendar import monthrange
def get_frac_of_month(date):
return 1. * (date.day - 1) / monthrange(date.year, date.month)[1]
def get_month_diff(date1, date2):
return date1.year * 12 + date1.month + get_frac_of_month(date1) - (date2.year * 12 + date2.month + get_frac_of_month(date2))
print get_month_diff(date1, date2)
6.16129032258