Given birth date in y/m/d format, write a Python program to find the present age in years.

**Examples:**

Input :1997/2/3Output :21 years (for present year i.e 2018)Input :2010/12/25Output :8 years (for present year i.e 2018)

**Approach # 1:**

The naive approach to find the current age in years is to find the difference between the current year and birth year. Refer the Naive appraoch from here.

**Approach #2:** Using datetime module

Python provides datetime module to deal with all datetime related issues in python. Using datetime we can find the age by subtracting birth year from current year. Along with this, we need to focus on the birth month and birthday. For this, we check if current month and date are less than birth month and date. If yes subtract 1 from age, otherwise 0.

`# Python3 code to calculate age in years ` ` ` `from` `datetime ` `import` `date ` ` ` `def` `calculateAge(birthDate): ` ` ` `today ` `=` `date.today() ` ` ` `age ` `=` `today.year ` `-` `birthDate.year ` `-` ` ` `((today.month, today.day) < ` ` ` `(birthDate.month, birthDate.day)) ` ` ` ` ` `return` `age ` ` ` `# Driver code ` `print` `(calculateAge(date(` `1997` `, ` `2` `, ` `3` `)), ` `"years"` `) ` |

*chevron_right*

*filter_none*

**Output:**

21 years

**Approach #3 :** Efficient datetime approach

The above approaches do not deal with a special case i.e. when birth date is February 29 and the current year is not a leap year. This case has to be raised as an exception because the calculation of birthdate may be inaccurate. This method includes try and catch for this exception.

`# Python3 code to calculate age in years ` `from` `datetime ` `import` `date ` ` ` `def` `calculateAge(born): ` ` ` `today ` `=` `date.today() ` ` ` `try` `: ` ` ` `birthday ` `=` `born.replace(year ` `=` `today.year) ` ` ` ` ` `# raised when birth date is February 29 ` ` ` `# and the current year is not a leap year ` ` ` `except` `ValueError: ` ` ` `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 ` ` ` `# Driver code ` `print` `(calculateAge(date(` `1997` `, ` `2` `, ` `3` `)), ` `"years"` `) ` |

*chevron_right*

*filter_none*

**Output:**

21 years

**Approach #4: ** Using division

In this approach, we calculate the number of date from the birth date till current date. Divide the number of date by the days in a year i.e 365.2425.

`# Python3 code to calculate age in years ` `from` `datetime ` `import` `date ` ` ` `def` `calculateAge(birthDate): ` ` ` `days_in_year ` `=` `365.2425` ` ` `age ` `=` `int` `((date.today() ` `-` `birthDate).days ` `/` `days_in_year) ` ` ` `return` `age ` ` ` `# Driver code ` `print` `(calculateAge(date(` `1997` `, ` `2` `, ` `3` `)), ` `"years"` `) ` |

*chevron_right*

*filter_none*

**Output:**

21 years

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Program to calculate age
- Program to check if a given year is leap year
- Program to calculate Dooms Day for a year
- Python: Age Calculator using Tkinter
- Python - Age Calculator using PyQt
- PyQt5 - Age Nearest Birthday Calculator
- Python Program for Program to calculate area of a Tetrahedron
- Python program to find the first day of given year
- Python program to print calendar of given year
- Program to find century for a year
- Python program to calculate the number of words and characters in the string
- Python program to calculate gross pay
- Python program to calculate the number of digits and letters in a string
- Python | Pandas DatetimeIndex.year
- Python | Pandas Period.year
- Python | Pandas PeriodIndex.year
- Python | Pandas Timestamp.year
- Python | Pandas Series.dt.year
- Python IMDbPY – Getting released year of movie from movie object
- Python IMDbPY – Getting year of the series

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.