Files
30-seconds-of-code/snippets/months-diff.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

573 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Date difference in months date succulent-11 2020-10-28T16:20:39+02:00 2020-10-28T16:20:39+02:00

Calculates the month difference between two dates.

  • Subtract start from end and use datetime.timedelta.days to get the day difference.
  • Divide by 30 and use math.ceil() to get the difference in months (rounded up).
from math import ceil

def months_diff(start, end):
  return ceil((end - start).days / 30)
from datetime import date

months_diff(date(2020, 10, 28), date(2020, 11, 25)) # 1