Python Age Calculator
Age calculation involves determining a age based on their date of birth and the current date. The process typically includes calculating the number of years, months, and days between the birthdate and the current date.
Age Calculation Mathematically
In this section we will calculate age mathematically, so that we know the basic rules of age calculation.
Easy Example:
Year/Month/Day
Current Date : 2024/05/30
Date of Birth : 2022/04/26
--------------------------
Age is : 2Y - 1M - 4D
Here :
This example is very much Easy. Here directly subtract:
-
Day :
30 - 26 = 4
-
Month :
05 - 04 = 1
-
Year :
2024 - 2022 = 2
and get the Answer of Age: 2 Years 1 Month 4 Days
Medium Example:
Year/Month/Day
Current Date : 2024/01/30
Date of Birth : 2022/04/26
--------------------------
Age is : 1Y - 9M - 4D
Here :
This example is very little bit complex. Here can not subtract directly:
-
Day :
30 - 26 = 4
-
Month :
01 - 04 = -3
>>(01 + 12) - 04 = 9
-
Here got negative months, but in reality month can’t be negative. That’s why here borrow 1 year from the current years.
-
But we need month not year, so year converted to month 1 x 12 = 12 months
-
-
Year :
2024 - 2022 = 2
>>(2024 - 1) - 2022 = 1
-
Here subtract 1 year because the year borrowed to the month.
-
and get the Answer of Age: 1 Years 9 Months 4 Days
Hard Example:
Year/Month/Day
Current Date : 2024/01/30
Date of Birth : 2022/03/31
--------------------------
Age is : 1Y - 9M - 29D
Here :
This example is very hard. Here directly subtract not possible:
-
Day :
30 - 31 = -1
>>(30 + 30) - 31 = 29
-
Borrow days from current month. Normally 1 month = 30 days counted, not consider 28, 29 or 31
-
-
Month :
01 - 03 = -2
>>(01 + 12 -1) - 03 = 9
-
Borrow months from current year and subtract the given month to days.
-
-
Year :
2024 - 2022 = 2
>>(2024 - 1) - 2022 = 1
-
Subtract year which given to month.
-
and get the Answer of Age: 1 Years 9 Months 29 Days
Note: During the borrow of month calculated as 30 days but in reality month can be 31, 30, 29, 28 days, that’s why added extra 1 in various age calculation math.
Python Age Calculation Simple Program
from datetime import date
def calculate_age(day, month, year, today=None):
date_of_birth = date(day=day, month=month, year=year)
if not today:
today = date.today()
year = today.year - date_of_birth.year
month = today.month - date_of_birth.month
day = today.day - date_of_birth.day
if day < 0:
month -= 1
day += 30
if month < 0:
year -= 1
month += 12
return year, month, day
if __name__ == "__main__":
year, month, day = calculate_age(31, 3, 2022, date(day=30, month=1, year=2024))
print(f"Age is: {year} years {month} month/s {day} day/s")
Python Age Calculation Advanced Program
from datetime import date
def get_days_in_month(month, year):
if month == 2: # February
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return 29 # Leap year
else:
return 28
elif month in [4, 6, 9, 11]: # April, June, September, November
return 30
else:
return 31
def calculate_age(day, month, year, today=None):
date_of_birth = date(day=day, month=month, year=year)
if not today:
today = date.today()
year = today.year - date_of_birth.year
month = today.month - date_of_birth.month
day = today.day - date_of_birth.day
if day < 0:
month -= 1
day += get_days_in_month(month, year)
if month < 0:
year -= 1
month += 12
return year, month, day
if __name__ == "__main__":
year, month, day = calculate_age(31, 3, 2022, date(day=30, month=1, year=2024))
print(f"Age is: {year} years {month} month/s {day} day/s")