Back to the tool

Why Your Date Validation Regex Fails on Real Dates

Here is a date: 2026-02-30. Run it against the date validation regex everyone copies from the internet and it passes. Every common pattern accepts it. That is not a bug in the pattern. It is the reason date patterns keep failing: regex checks shape, and "is this a real day on a real calendar" is not a shape question.

The pattern everyone uses looks like this:

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

It is a perfectly good shape check. Four digits, dash, month 01-12, dash, day 01-31. It rejects 2026-13-45 and hello-world. What it cannot do is know that February has 28 days this year, or that April never has a 31st, or that 2026-02-29 does not exist. A regex has no calendar. The day group allows 01 through 31 for every month because that is all it can express without turning into a monster.

Why your date validation regex fails: the leap-year problem

February is where this bites hardest, because leap years are a three-part rule: a year is a leap year if it is divisible by 4, except century years, which are leap years only if divisible by 400. So 2024 was a leap year, 2100 will not be one, and 2000 was one. You can, technically, encode all of this in regex. People have done it. The result is several hundred characters of alternation that nobody can read, debug, or trust, and it still does not handle month lengths.

This is the point where you stop asking regex to be a calendar. Every language ships a date parser that knows the Gregorian calendar: Python's datetime.strptime, JavaScript's Date with component verification, C#'s DateTime.TryParseExact. The parser handles leap years, month lengths, and the century rule for free, because it was written by people whose job was the calendar.

The two-step pattern that actually works

Use regex for what it is good at: rejecting garbage early, in one pass, before the parser touches it. Then parse. In JavaScript:

function isValidISODate(s) {
  if (!/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/.test(s)) return false;
  const [y, m, d] = s.split('-').map(Number);
  const dt = new Date(y, m - 1, d);
  return dt.getFullYear() === y && dt.getMonth() === m - 1 && dt.getDate() === d;
}

The regex runs first and rejects anything that is not shaped like a date. The Date construction then normalizes: feed it February 30 and it rolls forward to March 2, and the component check catches the rollover because the month no longer matches. That last comparison is the whole trick, and it is one line.

Same idea in Python: match the shape, then datetime.strptime(s, "%Y-%m-%d"), which raises on impossible dates. In C#: DateTime.TryParseExact with the explicit format and an invariant culture, which fails for 29/02/1900 the way it should.

Where this breaks down

Three cases, because the two-step answer has edges and I would rather name them than let you find them in production.

First, locale ambiguity. 03/04/2026 is March 4 in the US and April 3 in most of Europe. A regex cannot resolve that; a format string can, which is why the parse step should always use an explicit format, never the parser's guess. new Date("03/04/2026") in JavaScript is implementation-dependent, so split and construct instead.

Second, two-digit years. 26 could be 1926 or 2026 depending on the library's pivot year. If your input allows them, normalize before parsing, or better, refuse them at the regex step by requiring four digits.

Third, historical dates. The Gregorian calendar rules we use today were not in force everywhere before about 1582, and some libraries apply them proleptically, meaning backwards. If you validate dates before 1900, check what your library assumes. Excel, famously, still thinks 1900 was a leap year, a bug inherited from Lotus 1-2-3 and kept for compatibility. Your regex will never have that bug, because your regex has no calendar at all.

The failure mode repeats: the shape layer gets asked to do the truth layer's job.

Frequently asked questions

Why does my date regex accept invalid dates like 2026-02-30?

Because regex validates shape, not the calendar. A pattern like (0[1-9]|[12]\d|3[01]) allows day 30 in February because it cannot know how many days each month has. You need a real date parser to check whether the date exists.

What is the correct regex for ISO dates?

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ checks the shape of YYYY-MM-DD strings. It rejects bad months and out-of-range days but still accepts impossible dates like February 30, so follow it with a date parse.

How do I validate leap years in regex?

Do not. Leap years follow the rule: divisible by 4, except centuries, unless divisible by 400. Encoding that in regex produces a long, unreadable pattern. Parse the date with a real library instead, which handles leap years natively.

Should I validate dates with regex or a date library?

Use both, each for its job. Regex checks the input shape and rejects garbage early; a date library confirms the date exists on the calendar. Regex alone is never sufficient for validation.

Related reading: Email Regex Validation Mistakes · URL Validation Mistakes · Why There Is No Perfect Email Regex · Catastrophic Backtracking, Explained

Try the tool

Build and test the shape-check pattern against your own inputs. Build a regex pattern with plain-English explanations.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.