Home > OS >  Convert a date into an integer in python
Convert a date into an integer in python

Time:03-19

I'am trying to convert a date to an integer for a comparison.

# Python3 code to calculate age in years
import datetime
from datetime import date

date_entry = input('Enter your birthdate in YYYY/MM/DD format: ')
year, month, day = map(int, date_entry.split('/'))
date1 = datetime.date(year, month, day)

def calculateAge(birthDate):
    today = date.today()
    age = today.year - birthDate.year - \
        ((today.month, today.day) < (birthDate.month, birthDate.day))

    return age

# Driver code
print(calculateAge(date1), "years")

if date1 < 18:
    print('You are under age')
    exit()

I have an error in my if statement because date1 is not an integer. How can I solve this?

Thanks.

CodePudding user response:

you can edit as below.

if calculateAge(date1) < 18:
    print('You are under age')
    exit()

enter image description here

  • Related