将日期转换为 python 中的整数

Convert a date into an integer in python

我正在尝试将日期转换为整数以进行比较。

# 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()

我的 if 语句有错误,因为 date1 不是整数。 我该如何解决?

谢谢。

您可以进行如下编辑。

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

enter image description here