Check if a variable is a Datetime object in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# Check if a variable is a datetime object in Python

Use the isinstance built-in function to check if a variable is a datetime object in Python, e.g. if isinstance(today, datetime):.

The isinstance function returns True if the passed-in object is an instance or a subclass of the passed-in class.

main.py
from datetime import datetime today = datetime.today() print(today) if isinstance(today, datetime): # ๐Ÿ‘‡๏ธ this runs print('variable is datetime object') else: print('variable is NOT datetime object')

check if variable is datetime object

The code for this article is available on GitHub

The isinstance function returns True if the passed-in object is an instance or a subclass of the passed-in class.

We imported the datetime class from the datetime module.

The datetime class is a subclass of the date class, so make sure you pass the correct second argument to isinstance.

# A date object is not an instance of datetime

If you create a date object and check if it's an instance of datetime, you would get False back.

main.py
from datetime import datetime, date d = date(2022, 9, 24) if isinstance(d, datetime): print('variable is datetime object') else: # ๐Ÿ‘‡๏ธ this runs print('variable is NOT datetime object')

date object is not an instance of datetime

The code for this article is available on GitHub

The else block runs because date objects are not an instance of datetime, they are an instance of the date class.

main.py
from datetime import date d = date(2022, 9, 24) # ๐Ÿ‘‡๏ธ Pass date as second arg to isinstance if isinstance(d, date): # ๐Ÿ‘‡๏ธ this runs print('variable is datetime object') else: print('variable is NOT datetime object')

# Using the is operator to check if an object is a datetime object

You can also use the is operator to check if an object is a datetime object.

main.py
from datetime import datetime, date d = date(2022, 9, 24) print(type(d) is date) # ๐Ÿ‘‰๏ธ True today = datetime.today() print(type(today) is datetime) # ๐Ÿ‘‰๏ธ True

using is operator to check if object is datetime

The is and is not operators test for an object's identity: x is y is true only if x and y are the same object.

An object's identity is determined using the id function.

x is not y returns the inverse truth value.

The type class returns the type of an object.

main.py
from datetime import datetime, date d = date(2022, 9, 24) print(type(d)) # ๐Ÿ‘‰๏ธ <class 'datetime.date'> today = datetime.today() print(type(today)) # ๐Ÿ‘‰๏ธ <class 'datetime.datetime'>

type class returns type of object

The code for this article is available on GitHub

Most commonly the return value is the same as accessing the __class__ attribute on the object.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev