• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Check if String is Date in Python

Check if String is Date in Python

June 2, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Selenium maximize_window() Function to Maximize Window in Python
  • 2.  Convert String to Float with float() in Python
  • 3.  How to Group By Columns and Count Rows in pandas DataFrame
  • 4.  How to Remove All Occurrences of a Character in a List Using Python
  • 5.  How to Use Python to Remove Zeros from List
  • 6.  Creating a Random Color Turtle in Python
  • 7.  Clear File Contents with Python
  • 8.  Find Quotient and Remainder After Division in Python
  • 9.  Calculate Distance Between Two Points in Python
  • 10.  How to Write Python Dictionary to File

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · The Programming Expert · About · Privacy Policy