3. Python ways
from datetime import date
def calculate_age(born):
today = date.today()
try:
birthday = born.replace(year=today.year)
except ValueError: # raised when birth date is February 29 and the current year is not a leap year
birthday = born.replace(year=today.year, month=born.month+1, day=1)
if birthday > today:
return today.year - born.year - 1
else:
return today.year - born.year
4. Ruby & Rails Way
• from datetime import date
• def calculate_age(born):
• today = date.today()
• try:
• birthday = born.replace(year=today.year)
• except ValueError: # raised when birth date is February 29 and the current year is not a leap
year
• birthday = born.replace(year=today.year, month=born.month+1, day=1)
• if birthday > today:
• return today.year - born.year - 1
• else:
• return today.year - born.year
5. Java ways
LocalDate birthdate = new LocalDate (1970, 1, 20);
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);