• 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
  • Ruby
  • 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.  Writing a Table Faster to Word Document Using python-docx
  • 2.  Using Python to Convert Integer to String with Leading Zeros
  • 3.  Check if Number is Larger than N in Python
  • 4.  Using Python to Calculate Sum of List of Numbers
  • 5.  How to Check if String Contains Lowercase Letters in Python
  • 6.  Get First Key and Value in Dictionary with Python
  • 7.  Python Check if Attribute Exists in Object with hasattr() Function
  • 8.  Multiply Each Element in List by Scalar Value with Python
  • 9.  Python Replace Space with Underscore Using String replace() Function
  • 10.  Print Object Attributes in Python using dir() Function

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy

x