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
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ URL /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv4 Address /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/