How can I calculate the age of a person in year, month, days?

前端 未结 11 1197
谎友^
谎友^ 2020-11-27 05:52

I want to calculate the age of a person given the date of birth and the current date in years, months and days relative to the current date.

For example:

<         


        
11条回答
  •  无人及你
    2020-11-27 06:22

    This Algorithm prints the next format -> 24 Years, 8 Months,2 Days

    from dateutil.relativedelta import *
    from datetime import date
    
    def calc_age(dob):
      today = date.today()
      age = relativedelta(today, dob)
      print str(age.years)+" Years, "+str(age.months)+" Months,"+str(age.days)+" Days"
    
    #test it
    calc_age(date(1993,10,10))
    

提交回复
热议问题