Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Latest commit

 

History

History
25 lines (19 loc) · 553 Bytes

months-diff.md

File metadata and controls

25 lines (19 loc) · 553 Bytes
title type tags cover dateModified
Date difference in months
snippet
date
succulent-11
2020-10-28 16:20:39 +0200

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