Python: validating an instance of date

Viewed 58

I am trying to do a check if a variable is of type date using the below function:

from datetime import date, datetime
def verify_date_dt(in_date):
    f_date = in_date
    return isinstance(f_date, datetime.date)

I am calling this function from another file to check a date value -

in_date = date(2021,4,21)
print(verify_date_dt(in_date))

Am getting the below error -

TypeError: isinstance() arg 2 must be a type or tuple of types.

Would appreciate help. I am using Python 3.6.4

2 Answers

You imported datetime.date as date, and you imported datetime.datetime as datetime. So when you write datetime.date you end up with the date() method of datetime.datetime (which is, as the error complains, not a type).

Instead just write isinstance(f_date, date).

from datetime import date
def verify_date_dt(in_date):
    f_date = in_date
    return isinstance(f_date, date)


in_date = date(2021,4,21)
print(verify_date_dt(in_date))

Output:
True
Related