user18021056
user18021056

Reputation: 3

Convert a date into an integer in python

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.

Upvotes: 0

Views: 314

Answers (1)

Mert Kulac
Mert Kulac

Reputation: 294

you can edit as below.

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

enter image description here

Upvotes: 2

Related Questions