Regex for date (YYYY-MM-DD)

Use /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/ to check that a whole string is a valid for date (YYYY-MM-DD). Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/
Open in Regex Tester
InputResult
2026-10-30✓ Match
1999-01-01✓ Match
2024-02-29✓ Match
2026-13-01✗ No match
2026-1-5✗ No match
2026/10/30✗ No match
26-10-30✗ No match
2026-00-10✗ No match

How the pattern works

PartMeaning
^\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 false

Python

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.