Skip to content

Instantly share code, notes, and snippets.

@Johnetordoff
Created August 28, 2024 15:24
Show Gist options
  • Save Johnetordoff/bb6e4845a8a9405cc5250835337c5201 to your computer and use it in GitHub Desktop.
Save Johnetordoff/bb6e4845a8a9405cc5250835337c5201 to your computer and use it in GitHub Desktop.
from datetime import datetime
def is_leap_year(year):
return (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)
def calculate_leap_age(birthdate, current_date):
birthdate = datetime.strptime(birthdate, '%Y-%m-%d')
current_date = datetime.strptime(current_date, '%Y-%m-%d')
start_year = birthdate.year if birthdate.month > 2 else birthdate.year + 1
end_year = current_date.year - 1 if current_date.month < 2 or (current_date.month == 2 and current_date.day < 29) else current_date.year
return sum(is_leap_year(year) for year in range(start_year, end_year + 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment