ISO 8601 Date
beginnerMatches dates in YYYY-MM-DD format following ISO 8601.
Pattern
/\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])/gTry It
Explanation
Matches 4-digit year, followed by month (01-12) and day (01-31) separated by hyphens. Does not validate month-day combinations (e.g., Feb 31 passes).
When to Use
Use this pattern to extract or quick-validate dates in YYYY-MM-DD format from log files, CSV data, API responses, or form inputs. ISO 8601 is the international standard used in databases, REST APIs, and data interchange. Note: this pattern validates format and basic range only — it does not check calendar correctness (e.g., 2024-02-30 passes). Always parse with a date library after regex matching for full validation.
Step-by-Step Breakdown
| Token | Explanation |
|---|---|
\d{4} | Match exactly 4 digits — the year (e.g., 2024) |
- | Match a literal hyphen separating year from month |
(?:0[1-9]|1[0-2]) | Match month 01-12: either 0 followed by 1-9, or 1 followed by 0-2 |
- | Match a literal hyphen separating month from day |
(?:0[1-9]|[12]\d|3[01]) | Match day 01-31: 01-09, 10-29, or 30-31 |
Common Mistakes
Using \d{2} for the month instead of (?:0[1-9]|1[0-2])
Fix: (?:0[1-9]|1[0-2])\d{2} accepts invalid months like 00 and 13. The alternation constrains months to the valid 01-12 range.
Assuming the regex validates real calendar dates
Fix: Use Date.parse() or datetime.strptime() after matchingThis pattern cannot detect impossible dates like February 30 or April 31. Regex checks format; a date library checks calendar validity.
Not anchoring the pattern when validating a full string
Fix: ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$Without ^ and $ anchors, the pattern matches substrings — e.g., it finds '2345-01-01' inside '12345-01-01'. Add anchors when the entire input must be a date.
Test Strings
Matching
- 2024-01-15
- 2023-12-31
- 1999-06-30
Non-matching
- 2024-13-01
- 2024-00-15
- 24-01-15
- 2024/01/15
Language Compatibility
| Language | Support |
|---|---|
| JS | Full support |
| PYTHON | Full support |
| JAVA | Full support |
| PHP | Full support |
| GO | Full support |
| RUBY | Full support |
| CSHARP | Full support |
Code Snippets
const regex = /\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])/g;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);Common Variations
With slashes
\d{4}/(?:0[1-9]|1[0-2])/(?:0[1-9]|[12]\d|3[01])YYYY/MM/DD format
Related Patterns
Learn the Concepts
This pattern uses concepts covered in these lessons: