How To Check If A Variable Is A Datetime Object In Python

Check if a variable is a datetime object in Python

Today’s topic I bring to you is to check if a variable is a datetime object in Python. Are there ways to do that? I use the isinstance function, the is operator, and try/except block to test. Follow the following article for more details.

Check if a variable is a datetime object in Python

Datetime is a library for working with date and time in Python. This is one of the most used libraries in Python. Commonly used modules in datetime are date object, time object, datetime object, and timedelta object.

Use the isinstance() function.

Syntax:

isinstance(object , classinfo)

Parameters:

  • object: the number you want to check.
  • classinfo: class, type, or tuple.

Example:

import datetime

var1 = '2022-06-14'
var2 = datetime.date(2022, 6, 14)

# Using the isinstance function, check the datetime object.
print('Is the object a datetime object?', isinstance(var1, datetime.date))
print('Is the object a datetime object?', isinstance(var2, datetime.date))

Output:

Is the object a datetime object? False
Is the object a datetime object? True

The variable ‘var1’ contains a string value type so that the isinstance function will return False.

The variable ‘var2’ contains a datetime object so that the isinstance function will return True.

Or you can check the datetime object with the isinstance function and then return a positive or negative statement.

Example:

import datetime

var1 = '2022-6-14'
var2 = datetime.date(2022, 6, 14)

if isinstance(var1, datetime.date):
    print('var1 is a datetime object.')
else:
    print('var1 is not a datetime object.')
    
if isinstance(var2, datetime.date):
    print('var2 is a datetime object.')
else:
    print('var2 not a datetime object.')

Output:

var1 is not a datetime object.
var2 is a datetime object.

Use the is operator.

Example:

  • Using the type function in conjunction with the is operator checks whether an object is a datetime.
import datetime

myVar = datetime.date(2022, 6, 14)

# Using the type() function in conjunction with the is operator checks the datetime object.
if type(myVar) is datetime.date:
    print('The data type of myVar:', type(myVar))
    print('myVar is a datetime object.')
else:
    print('myVar is not a datetime object.')

Output:

The data type of myVar: <class 'datetime.date'>
myVar is a datetime object.

Use try/except.

Example:

import datetime

myVar = datetime.date(2022, 6, 14)

try:
  # Using the isinstance function to check the datetime object.
    if isinstance(myVar, datetime.date):
        print('myVar is datetime object')
except:
    print('myVar is not datetime object')

Output:

myVar is datetime object

The code between the first try-except clause is executed. If no exception occurs, only the try clause will run. The except clause will not run. If a KeyError exception occurs, only the except clause runs.

Summary

Those are some ways to check if a variable is a datetime object in Python. The proper way to do this is using the isinstance() function. What do you think about this topic? Let me know what you think by leaving a comment.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *