0
bdate = input("Type your Date of birth (ie.10/11/2011) : ")
print(bdate)
day, month, year = map(int, bdate.split('/'))
birth_date = datetime.date(day, month, year)
print(birth_date)
today = datetime.datetime.now().strftime("%Y")
print(today)
age = today - birth_date.year ```

Error : day is out of range for month how to solve this error

2 Answers2

0

try this, using relativedelta

from dateutil.relativedelta import relativedelta
from datetime import datetime

bdate = input("Type your Date of birth (ie.10/11/2011) : ")

# convert the input string to datetime-object.
birth_date = datetime.strptime(bdate, "%d/%m/%Y")

print(f"{relativedelta(datetime.now(), birth_date).years} yrs")
sushanth
  • 8,275
  • 3
  • 17
  • 28
0

Like @sushanth says you can use relativedelta.

But to understand what was wrong about your code I have corrected it:

import datetime

bdate = input("Type your Date of birth (ie.10/11/2011) : ")

day, month, year = map(int, bdate.split('/'))
birth_date = datetime.date(year, month, day)

current_year = datetime.datetime.now().year

age = current_year - birth_date.year

print(age)

The first problem is, that datetime.date takes the following attributes: year, month, day not day, month, year.

The second problem is that you cannot subtract a string from an integer. Instead you can use datetime.datetime.now().year to get the current year (int).

Jens Becker
  • 1,471
  • 1
  • 7
  • 11