JavaScript / ECMAScript

ISO 8601 Date in JS

Match dates in ISO 8601 format: YYYY-MM-DD with valid month (01–12) and day (01–31) ranges.

Try it in the JS tester →

Pattern

regexJS
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])", "g");
const input = "2024-01-15";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

Year is any 4-digit number. Month uses alternation to enforce 01-12. Day enforces 01-31, though it won't catch month-specific overflows (e.g. Feb 30).

Examples

Input

2024-01-15

Matches

  • 2024-01-15

Input

1999-12-31

Matches

  • 1999-12-31

Input

2024-13-01

No match

Same pattern, other engines

← Back to ISO 8601 Date overview (all engines)