Regex for ISO Date (YYYY-MM-DD)
Matches dates in YYYY-MM-DD format (basic validation).
Understanding The Regex Pattern
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/- 2023-12-25
- 1999-01-01
- 2023-13-01
- 23-01-01
- 2023/12/25
How this Pattern Works
The ISO 8601 date format (YYYY-MM-DD) is the universally recognized and globally accepted standard for representing calendar dates. It was designed specifically to avoid the persistent ambiguity and confusion caused by varying regional date formats, such as the American MM/DD/YY versus the European DD/MM/YY. This regular expression provides immediate, basic validation for this standard format, rigorously checking the structural components of the date string.
The pattern ensures that the provided input strictly contains a four-digit year, followed by a hyphen, a valid two-digit month (ranging from 01 to 12), another hyphen, and a valid two-digit day (ranging from 01 to 31). This level of structural validation is incredibly useful for standardizing data before it reaches your backend systems or database schemas.
While this regular expression serves as an excellent, high-performance first-pass filter for data entry forms, calendar widgets, and API payloads, it is important to understand its limitations. It does not compute complex calendar logic, meaning it will technically accept mathematically invalid dates like February 30th or February 29th on a non-leap year. For absolute date accuracy, this regex should be used on the frontend for immediate UI feedback, while your backend language's native Date parsing library should handle the final, authoritative calendar verification.
Implement The Regex In Your Code
const regex = /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/;
regex.test('2023-12-25');import re
regex = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$')
bool(regex.match('2023-12-25'))import "regexp"
matched, _ := regexp.MatchString(`^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$`, "2023-12-25")use regex::Regex;
let re = Regex::new(r"^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$").unwrap();
re.is_match("2023-12-25")