Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for date (YYYY-MM-DD)
Regex pattern · validation

Regex for date (YYYY-MM-DD)

Regex for date YYYY-MM-DD: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/ as a date (YYYY-MM-DD) regex. It matches "2026-11-25" and "1999-01-01", and rejects "2026-13-01" and "2026-11-32". Regex cannot know month lengths or leap years, so 2026-02-31 passes.

Last updated · Maintained by FreeAIToolz · How we test

/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/
  • 2026-11-25matches
  • 1999-01-01matches
  • 2024-02-29matches
  • 2026-13-01no match
  • 2026-11-32no match
  • 26-11-25no match
  • 2026/11/25no match
  • 2026-1-5no match
Open in the full Regex Tester (replace, flags, groups) →

How this regex 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

Examples

InputResult
2026-11-25✓ Matches
1999-01-01✓ Matches
2024-02-29✓ Matches
2026-13-01✗ No match
2026-11-32✗ No match
26-11-25✗ No match
2026/11/25✗ No match
2026-1-5✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Regex cannot know month lengths or leap years, so 2026-02-31 passes. Parse the date afterwards (for example with Date or datetime) to reject impossible days.

When to use this pattern

API parameters, CSV imports and ISO 8601 date fields.

Use it in your language

JavaScript

const re = /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$`)
ok := re.MatchString(value)

More regex patterns

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])$/. Regex cannot know month lengths or leap years, so 2026-02-31 passes. Parse the date afterwards (for example with Date or datetime) to reject impossible days.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abc2026-11-25xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.