Date/Time
ISO 8601 Date (YYYY-MM-DD) Regex
ISO 8601 dates are written year first with dashes, as in 2026-11-30. The format sorts correctly as plain text and is the default for JSON APIs, HTML date inputs, databases and file names. This pattern requires a four-digit year, a month from 01 to 12 and a day from 01 to 31, capturing year, month and day in groups 1 to 3. It is anchored, so the input must contain only the date. The honest limit is that it does not check the number of days in each month, so 2026-02-30 still passes the format check.
Pattern (JavaScript)
/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/Starts at the start of the line/string, then a group (a digit, exactly 4 times), then the character "-", then a group (the character "0", then one of the characters 1-9 OR the character "1", then one of the characters 0-2), then the character "-", then a group (the character "0", then one of the characters 1-9 OR one of the characters 12, then a digit OR the character "3", then one of the characters 01), then the end of the line/string.
Limit: Checks the format only; it does not check calendar validity like 31 February or leap years.
Examples
Matches
- ✓ 2026-11-30
- ✓ 2000-01-01
- ✓ 1999-12-31
Does not match
- ✗ 2026-13-01
- ✗ 2026/11/30
- ✗ 26-11-30
Try it
0 matches (JavaScript engine)
2026-11-30
2000-01-01
1999-12-31
2026-13-01
2026/11/30
26-11-30Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/g;
const text = "2026-11-30";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$')
text = '2026-11-30'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/';
$text = '2026-11-30';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$");
String text = "2026-11-30";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$`)
text := "2026-11-30"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$");
var text = "2026-11-30";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$").unwrap();
let text = "2026-11-30";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/
text = '2026-11-30'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is ISO 8601 recommended for storing dates?
- It is unambiguous across countries and sorts correctly as a string. Comparing two ISO dates alphabetically gives the same order as comparing them chronologically.
- Does this accept the basic format 20261130?
- No, it only accepts the extended format with dashes. Make each dash optional with -? if you also want the compact form.
- How do I find ISO dates inside text?
- Replace the anchors with \b and add the g flag. Version strings or IDs shaped like dates may also match, so review the surrounding context.
Related patterns
- ISO 8601 Date-Time^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9](\.[0-9]{1,9})?)?(Z|[+-]([01][0-9]|2[0-3]):?[0-5][0-9])?$
- Date DD/MM/YYYY^(0[1-9]|[12][0-9]|3[01])[/.-](0[1-9]|1[0-2])[/.-]([0-9]{4})$
- Date with Month Name^(0?[1-9]|[12][0-9]|3[01]) (?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Sep(?:t|tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?),? ([0-9]{4})$
- Month Name\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Sep(?:t|tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\b