Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can edit as below.

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

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading