Back to 30 Seconds Of Code

Calculate date difference

content/snippets/python/s/date-difference.md

14.0.0640 B
Original Source

Calculating the date difference between two dates in Python is a simple as subtracting one date from another. Then, you can use datetime.timedelta.days, combined with math.ceil() for months, to get the difference in days, months, or years.

py
def days_diff(start, end):
  return (end - start).days

def months_diff(start, end):
  return ceil((end - start).days / 30)

def years_diff(start, end):
  return ceil((end - start).days / 365)

from datetime import date

days_diff(date(2020, 10, 25), date(2020, 10, 28)) # 3
months_diff(date(2020, 10, 25), date(2020, 11, 25)) # 1
years_diff(date(2020, 10, 25), date(2021, 10, 25)) # 1