• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
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.  Python if else do nothing – Using pass Statement to Do Nothing in Python
  • 2.  pandas Drop Rows – Delete Rows from a DataFrame
  • 3.  How to Ask a Question in Python
  • 4.  Find Index of Maximum in List Using Python
  • 5.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 6.  Python Delete Variable – How to Delete Variables with del Keyword
  • 7.  How to Check if Variable Exists in Python
  • 8.  Sort List of Tuples in Python
  • 9.  Using Python to Count Number of False in List
  • 10.  Difference Between // and / When Dividing Numbers in Python

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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