Check if a string is a valid date format in Python
Codemia Knowledge Hub ·
Call datetime.strptime(value, fmt) and catch ValueError. That one call checks both things you care about: the text matches the expected format, and it is a real calendar date. Regex alone only covers the first.
Validating with datetime.strptime
strptime() parses according to a format string and raises ValueError if the input is malformed or impossible:
2026-02-31 fails even though it looks like a date, because February has no 31st day.
Why regex is not enough
A regex can confirm the string looks like YYYY-MM-DD, but it cannot easily tell whether the date is real:
The second value matches but is not a valid date. If you use a regex, treat it as a shape pre-filter and still pass the string to strptime().
Enforcing the exact format with a round trip
Some inputs parse successfully without matching the exact format you require. To reject them, parse and then format back to a string:
This suits APIs, CSV imports, and forms where canonical formatting matters as much as calendar correctness.
Accepting several explicit formats
If you accept more than one format, list them explicitly rather than falling back to fuzzy parsing, unless ambiguity is truly acceptable:
Anyone reading the code can see exactly which formats are allowed.
Returning an error message instead of False
A boolean is fine for quick filters, but callers often need the parsed date or a reason for the failure:
This helps in data-import tools, where you want to report why a row failed instead of silently dropping it.
Date-only versus date-time inputs
Decide early whether the contract is date-only or a full timestamp. If the input can include hours, minutes, or time zone information, validate against that full format rather than trimming the value:
Mixing date-only and date-time rules in one validator creates confusing edge cases.
Related reading
- How do I write good/correct package __init__.py files
- How do I write JSON data to a file?
- How do Python dictionary hash lookups work?
- How do Python functions handle the types of parameters that you pass in?
- How do Python's any and all functions work?
- How do threads work in Python, and what are common Python-threading specific pitfalls?
- How do we determine the number of days for a given month in python
- How do you access the query string in Flask routes?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.