Pythonic difference between two dates in years?

If you want precise results, I recommend using the dateutil library.

from dateutil.relativedelta import relativedelta
difference_in_years = relativedelta(end_date, start_date).years

This is for complete years (e.g. a person’s age). If you want fractional years, then add months, days, hours, … up to the desired precision.

Leave a Comment