calculate the difference between two datetime.date() dates in years and months

If you are able to install the excellent dateutil package, you can do this:

>>> from dateutil import relativedelta as rdelta
>>> from datetime import date
>>> d1 = date(2001,5,1)
>>> d2 = date(2012,1,1)
>>> rd = rdelta.relativedelta(d2,d1)
>>> "{0.years} years and {0.months} months".format(rd)
'10 years and 8 months'

Leave a Comment