This example describes how Python returns age based on the date of birth. Share to everyone for your reference. The implementation method is as follows:
def calculateage (self, date): "Calculates the age and" until next birthday from the given birth Date ' try: Date = Date.split ('. ') BirthDate = datetime.date (int (date[0]), int (date[1]), int (date[2])) today = Datetime.date.today () if (Today.mont H > Birthdate.month): nextyear = datetime.date (today.year + 1, birthdate.month, Birthdate.day) elif (today. Month < Birthdate.month): Nextyear = Datetime.date (today.year, Today.month + (birthdate.month-today.month), Bi Rthdate.day) elif (today.month = = Birthdate.month): if (Today.day > Birthdate.day): nextyear = Dat Etime.date (today.year + 1, birthdate.month, Birthdate.day) elif (Today.day < Birthdate.day): Nextyear = Datetime.date (Today.year, Birthdate.month, Today.day + (Birthdate.day-today.day)) elif (Today.day = = BIRTHDATE.D AY): nextyear = 0 age = today.year-birthdate.year if nextyear = = 0: #if Today is the birthday Return '%d, days until%d:%d ' percent (age, age+1, 0) else:daysleft = nextyear-today return '%d, Days until%d:%d '% (age, age+1, daysleft.days) except:return ' Wrong date format '
Here's how to use it:
Print checkdate (' 2000.05.05 ')
Hopefully this article will help you with Python programming.