To check if a string is a date, you can use the Python strptime() function from the datetime module. strptime() takes a string and a date format.

from datetime import datetime

string = "06/02/2022"

format_ddmmyyyy = "%d/%m/%Y"
format_yyyymmdd = "%Y-%m-%d"

try:
    date = datetime.strptime(string, format_ddmmyyyy)
    print("The string is a date with format " + format_ddmmyyyy)
except ValueError:
    print("The string is not a date with format " + format_ddmmyyyy)

try:
    date = datetime.strptime(string, format_yyyymmdd)
    print("The string is a date with format " + format_yyyymmdd)
except ValueError:
    print("The string is not a date with format " + format_yyyymmdd)

#Output:
The string is a date with format %d/%m/%Y
The string is not a date with format %Y-%m-%

When working with strings in Python, the ability to check if a string is a date can be very useful.

You can check if a string is a date using the Python strptime() function from the datetime module.

strptime() takes a string and a date format, and tries to create a datetime object. If the string matches the given string format, then the datetime object is created. If not, a ValueError occurs.

You can use a try-except block to try to create a datetime object with strptime() and if it succeeds, then you know the string is a date.

Below is a simple example showing you how to check if a string is a date in your Python code.

from datetime import datetime

string = "06/02/2022"

format_ddmmyyyy = "%d/%m/%Y"
format_yyyymmdd = "%Y-%m-%d"

try:
    date = datetime.strptime(string, format_ddmmyyyy)
    print("The string is a date with format " + format_ddmmyyyy)
except ValueError:
    print("The string is not a date with format " + format_ddmmyyyy)

try:
    date = datetime.strptime(string, format_yyyymmdd)
    print("The string is a date with format " + format_yyyymmdd)
except ValueError:
    print("The string is not a date with format " + format_yyyymmdd)

#Output:
The string is a date with format %d/%m/%Y
The string is not a date with format %Y-%m-%

How to Check if String has Specific Date Format in Python

If you want to check if a string is a date, you need to pass strptime() the correct date format.

There are a number of format codes which allow you to create different date and time formats.

You can check if a string is a specific date format by building a date format with the format codes linked above and then use strptime().

For example, if you want to check that a string is a date with format YYYYMMDD, you can use the format “%Y-%m-%d”.

string = "2022-06-02"

format_YYYYMMDD = "%Y-%m-%d"

try:
    date = datetime.strptime(string, format_YYYYMMDD)
    print("The string is a date with format " + format_YYYYMMDD)
except ValueError:
    print("The string is not a date with format " + format_YYYYMMDD)

#Output:
The string is a date with format %Y-%m-%d

Hopefully this article has been useful for you to learn how to use strptime() to check if a string is a date in Python.

Categorized in:

Python,

Last Update: February 26, 2024