If people type dates by hand, you’ll get values like 02/30/2026, 13/01/2026 and N/A. So check if a string is a valid date in Python before you use it.
A simple regex, \d{2}/\d{2}/\d{4}, only checked the shape, so February 30 slipped through.
Quick answer: pass the string to datetime.strptime(text, "%m/%d/%Y") inside try. A real date returns a datetime; anything else raises ValueError.
In this tutorial, I’ll show how to check if a string is a valid date in Python from strict formats to free text. Every VS Code screenshot is a real Python 3.12.5 run.
The Lease Dates We’ll Check
You’ll reuse seven dates from that rental form, saved as dates.py:
# Lease start dates typed into a Chicago rental form
LEASE_DATES = ["07/04/2026", "7/4/2026", "02/29/2026",
"02/30/2026", "13/01/2026", "2026-07-04", "N/A"]
Only two are really July 4, 2026. Once a string passes, converting a string to a date in Python is one more line.
Check if a String Is a Date in Python with strptime()
Same seven lease dates, checked by is_valid_date():
from datetime import datetime
from dates import LEASE_DATES
def is_valid_date(text, fmt="%m/%d/%Y"):
try:
datetime.strptime(text, fmt)
return True
except ValueError:
return False
for text in LEASE_DATES:
print(f"{text!r:>13} -> {is_valid_date(text)}")
Output:
'07/04/2026' -> True
'7/4/2026' -> True
'02/29/2026' -> False
'02/30/2026' -> False
'13/01/2026' -> False
'2026-07-04' -> False
'N/A' -> False

strptime() only returns when the format matches and the date exists. But 7/4/2026 passed: %m and %d accept one digit. Catch only ValueError; wider nets hide bugs (catching multiple exceptions in Python).
How Do You Check if a Date Is Valid (Feb 30, Leap Years)?
Print the ValueError message to see why a date failed:
from datetime import datetime
for text in ["02/29/2026", "02/29/2028", "02/30/2026",
"13/01/2026", "07/04/2026 "]:
try:
day = datetime.strptime(text, "%m/%d/%Y").date()
print(f"{text!r:>13} valid: {day:%A, %B %d, %Y}")
except ValueError as err:
print(f"{text!r:>13} invalid: {err}")
Output:
'02/29/2026' invalid: day is out of range for month
'02/29/2028' valid: Tuesday, February 29, 2028
'02/30/2026' invalid: day is out of range for month
'13/01/2026' invalid: time data '13/01/2026' does not match format '%m/%d/%Y'
'07/04/2026 ' invalid: unconverted data remains:

2026 isn’t a leap year, so 02/29/2026 fails while 02/29/2028 passes. You’ll find format codes like %B in converting a string to datetime in Python.
Check the Date Format Strictly in Python (MM/DD/YYYY)
Two-digit months and days only? Check the shape with a regex, then the calendar:
import re
from datetime import datetime
from dates import LEASE_DATES
SHAPE = re.compile(r"\d{2}/\d{2}/\d{4}")
def is_strict_date(text):
if not SHAPE.fullmatch(text):
return False # wrong shape: 7/4/2026, 2026-07-04
try:
datetime.strptime(text, "%m/%d/%Y")
return True
except ValueError:
return False # right shape, impossible date
for text in LEASE_DATES:
print(f"{text!r:>13} -> {is_strict_date(text)}")
Output:
'07/04/2026' -> True
'7/4/2026' -> False
'02/29/2026' -> False
'02/30/2026' -> False
'13/01/2026' -> False
'2026-07-04' -> False
'N/A' -> False

fullmatch() rejects extra text, and strptime() rejects 02/30/2026. More patterns are in my regular expressions in Python tutorial.
If you accept several formats, loop over them and return the first match. For ISO text like 2026-07-04T09:30:00, datetime.fromisoformat() is quicker. Valid dates can then feed a days-between calculation.
Validate Free-Form Date Strings with dateutil
dateutil.parser.parse() guesses the format of text like “Jul 4 2026 9:30 AM” (pip install python-dateutil):
from dateutil import parser
for text in ["July 4, 2026", "Jul 4 2026 9:30 AM", "2026-02-30",
"4", "N/A"]:
try:
print(f"{text!r:>22} -> {parser.parse(text)}")
except parser.ParserError as err:
print(f"{text!r:>22} -> invalid ({err})")
Output:
'July 4, 2026' -> 2026-07-04 00:00:00
'Jul 4 2026 9:30 AM' -> 2026-07-04 09:30:00
'2026-02-30' -> invalid (day is out of range for month: 2026-02-30)
'4' -> 2026-11-04 00:00:00
'N/A' -> invalid (Unknown string format: N/A)

On November 26, 2026, "4" became 2026-11-04, using today’s month. Use it to clean messy text, never as a strict check. For CSV or Excel columns, pandas to_datetime with errors="coerce" marks bad rows NaT.
Why Good Dates Fail Your Python Date Check
- Trailing spaces: “07/04/2026 ” raises “unconverted data remains”; call
.strip(). - Day-first input: 04/07/2026 from a UK user is April 7 under
%m/%d/%Y. - Two-digit years: “7/4/26” needs
%y, not%Y.
Checking a date string in Python comes down to strptime() in a try block, plus a regex for exact formats. You’ll like these next:
- Check for invalid dates in TypeScript
- Subtract days from a date in Python
- Convert a datetime to a Unix timestamp in Python
- Get the current date and time in Python
Questions About Checking Dates in Python
How do I check if a string is a date in Python?
Call datetime.strptime(text, fmt) in a try block; ValueError means no.
How do I validate a date format like MM/DD/YYYY in Python?
Match r"\d{2}/\d{2}/\d{4}" with re.fullmatch(), then call strptime().
Does strptime() catch February 30 and leap years?
Yes. 02/29/2026 raises “day is out of range for month”; 02/29/2028 passes.
How do I check if a string is a datetime in Python?
Use datetime.fromisoformat(). On 3.11+ it accepts 2026-07-04T09:30:00Z and rejects 2026-7-4.
How do I check a whole pandas column of dates?
Use pd.to_datetime() with errors="coerce"; bad rows become NaT.
Where are the strptime format codes documented?

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn