简体   繁体   中英

calculate age in days with python

as part of a course i have to define the age in days. the first 3 parts are working but the last is not. in that i have to import the two dates(age_in_days) and date of today in the previous function. could anyone explain how this works

import datetime
def days_in_month(year, month):
    """
    Inputs:
      year  - an integer between datetime.MINYEAR and datetime.MAXYEAR
              representing the year
      month - an integer between 1 and 12 representing the month

    Returns:
      The number of days in the input month.
    """
    if month==12:
        return 31
    else:
        date1 = datetime.date(year, month, 1)
        date2 = datetime.date(year, month+1, 1)
        difference = date2 - date1
        return(difference.days) 
#print(days_in_month(2012,2))

def is_valid_date(year, month, day):
    """
    Inputs:
      year  - an integer representing the year
      month - an integer representing the month
      day   - an integer representing the day

    Returns:
      True if year-month-day is a valid date and
      False otherwise
    """
    if datetime.MINYEAR<=year<=datetime.MAXYEAR and 1<=month<=12 and 1<=day<= days_in_month(year, month):
        return True
    else:
        return False
#print(is_valid_date(2012,10,21))

def days_between(year1, month1, day1, year2, month2, day2):
    """
    Inputs:
      year1  - an integer representing the year of the first date
      month1 - an integer representing the month of the first date
      day1   - an integer representing the day of the first date
      year2  - an integer representing the year of the second date
      month2 - an integer representing the month of the second date
      day2   - an integer representing the day of the second date

    Returns:
      The number of days from the first date to the second date.
      Returns 0 if either date is invalid or the second date is
      before the first date.
    """
    date1=datetime.date(year1,month1,day1)
    date2=datetime.date(year2,month2,day2)
    if is_valid_date(year1,month1,day1) and is_valid_date(year2,month2,day2)and (date1<date2):
        difference=date2-date1
        #print(difference.days)
        return difference.days

    else:        
        return 0 


def age_in_days(year, month, day):
    """
    Inputs:
      year  - an integer representing the birthday year
      month - an integer representing the birthday month
      day   - an integer representing the birthday day

    Returns:
      The age of a person with the input birthday as of today.
      Returns 0 if the input date is invalid or if the input
      date is in the future.
    """
    todays_date=datetime.date.today()
    if is_valid_date(year, month,day) and (age_in_days<todays_date):
        return days_between(age_in_days(year,month,day),todays_date(year,month,day))
    else:
        return 0

This will return days between 2 dates, specifically between the birth and the current date:

import datetime
birthday = datetime.date(1990,2,10) #datetime.date(YEAR,MONTH,DAY)
now = datetime.date.today()
delta = now - birthday
print(delta.days)

age_in_days is a function. It cannot be compared with todays_date which is an object. Again, in return statement todays_date cannot be passed like that.

def age_in_days(year,month,day):
    todays_date = dt.date.today()
    birthdate = dt.date(year,month,day)
    if is_valid(year,month,day) and (todays_date>birthdate):
        return days_in_between(todays_date.year,todays_date.month,todays_date.day,birthdate.year,birthdate.month,birthdate.day)
    else:
        return 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM