T
ToolPrime

Regex for Date (YYYY-MM-DD)

Validates dates in ISO 8601 format with basic month/day range checking.

Pattern

/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/

Live Tester

Enter a string to test

Examples

2024-01-15
2023-12-31
2000-06-01
2024-13-01
2024-00-15
24-01-15

Pattern Breakdown

^\d{4} — four-digit year

(0[1-9]|1[0-2]) — month 01-12

(0[1-9]|[12]\d|3[01])$ — day 01-31

Code Snippets

Javascript

const regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
regex.test("2024-01-15"); // true

Python

import re
pattern = r"^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$"
bool(re.match(pattern, "2024-01-15"))  # True

Php

$pattern = '/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/';
preg_match($pattern, "2024-01-15"); // 1

Related Patterns