The catch: February 30 matches
A regex only checks the shape of each part, so 2026-02-30 and 2026-04-31 pass. Encoding the number of days in each month and leap years in a regex is possible but unreadable. Check the shape with the regex, then check the date itself:
const ISO_DATE = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
function isValidDate(value) {
if (!ISO_DATE.test(value)) return false;
const [year, month, day] = value.split("-").map(Number);
const date = new Date(Date.UTC(year, month - 1, day));
// Invalid days roll over (Feb 30 becomes Mar 2), so compare the parts back.
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
}
isValidDate("2028-02-29"); // true (leap year)
isValidDate("2026-02-30"); // falseOther date formats
05/04/2026 is 5 April in most of the world and May 4 in the US. When you control the format, use YYYY-MM-DD: it's unambiguous and sorts correctly as text.
| Format | Pattern |
|---|---|
| DD/MM/YYYY | ^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$ |
| MM/DD/YYYY | ^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$ |
| ISO date and time (UTC) | ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$ |
Frequently asked questions
Why doesn't 2026-1-5 match?
The pattern requires two-digit months and days, as ISO 8601 does. Pad them with zeros, 2026-01-05, or change (0[1-9]|1[0-2]) to (0?[1-9]|1[0-2]) to allow one digit.
Can I use named groups to read the parts?
Yes: ^(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])$, then read match.groups.year and so on.
Is new Date("2026-10-19") safe to use?
A date-only ISO string is parsed as midnight UTC, so in time zones west of UTC it shows as the previous day. Build the date with Date.UTC as above, or keep it as a string.