Reputation: 19
I want to calculate age. I am going to receive the birthday from user and calculate the age. I think I need to convert the Input type into date format. Can someone helps me?
I tried to make a list of [year, month, day]. They are integers and I used the following formula to calculate the age but I ran into the error(int type in callable).
Age=(Now.year-Birthday[0])-(Now.month,Now.day)<(Birthday[1],Birthday[2])
Upvotes: 0
Views: 1168
Reputation: 166
By importing datetime
class (from datetime
module, they're called the same, it's confusing) you can use the strptime() to translate dates from string to Date:
from datetime import datetime
import math
bday_str = '18/04/1998'
bday_date = datetime.strptime(bday_str, '%d/%m/%y')
This would get you '18/04/1998' as a Date object, which later on you can use to operate to get the age of the person:
now = datetime.datetime.now()
age = math.trunc((now-bday_date).days/365.2425)
print(age)
>>> 22
Upvotes: 1