How the pattern works
| Part | Meaning |
|---|---|
^\d{4} | Four-digit year |
(?:0[1-9]|1[0-2]) | Month 01–12 |
(?:0[1-9]|[12]\d|3[01]) | Day 01–31 |
- | Literal dashes between the parts |
Code examples
JavaScript
const re = /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/;
re.test(value); // true or falsePython
import re
bool(re.fullmatch(r'\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])', value))PHP
preg_match('/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/', $value) === 1;
Notes and limitations
A regex can check the format but not the calendar: 2026-02-30 passes. After matching, confirm with a date library or new Date(value) and compare the parts. This is the ISO 8601 format used by <input type="date">.
Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool Frequently asked questions
What is the regex for date (YYYY-MM-DD)?
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: 2026-10-30, 1999-01-01, 2024-02-29. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.