Complete Guide 5 min read

Regex for Dates: DD/MM/YYYY, MM/DD/YYYY and YYYY-MM-DD

Tested regex patterns for DD/MM/YYYY, MM/DD/YYYY and YYYY-MM-DD dates, how to find dates inside text, reorder them, and why regex can't catch 31 February.

regex for date dd/mm/yyyydate regexregex yyyy-mm-ddregex date validationregex mm/dd/yyyyjavascript date regex

The short answer

For DD/MM/YYYY, use ^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/\d{4}$. For YYYY-MM-DD, use ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$. These check the format and the day and month ranges, but no regex will reliably reject dates such as 31/02/2026, so confirm the real date in code afterwards. Paste any pattern below into the Regex Tester with your own samples to see exactly what it matches.

The building blocks

Every date pattern on this page is built from three small pieces:

  • Day 01 to 31: (0[1-9]|[12][0-9]|3[01]) means 0 followed by 1 to 9, or 1 or 2 followed by any digit, or 3 followed by 0 or 1.
  • Month 01 to 12: (0[1-9]|1[0-2]) means 0 followed by 1 to 9, or 1 followed by 0, 1 or 2.
  • Four-digit year: \d{4} means any four digits. Use (19|20)\d{2} if you only accept years 1900 to 2099.

^ and $ anchor the pattern to the start and end of the input, so "abc25/12/2026" is rejected. The slashes are written as \/ so the pattern also works inside JavaScript's /pattern/ syntax.

DD/MM/YYYY (India, UK and most of the world)

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

InputResultWhy
25/12/2026MatchValid day and month
5/1/2026No matchDay and month must have two digits
00/01/2026No matchThere is no day 00
01/13/2026No matchThere is no month 13
12/25/2026No match25 is not a valid month
31/02/2026MatchCorrect shape, but not a real date

Allowing one-digit days and months

Forms often receive 5/1/2026 as well as 05/01/2026. Make the leading zero optional with 0?:

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

Allowing slashes, dashes or dots, used consistently

To accept 05/01/2026, 05-01-2026 and 05.01.2026 but reject a mix such as 5/1-2026, capture the first separator and repeat it with a back-reference, \2:

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

In a test, this matched 5/1/2026, 05-01-2026 and 05.01.2026, and rejected 5/1-2026. A simpler version with [\/.-] in both places would accept the mixed one.

MM/DD/YYYY (United States)

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

This is the same pattern with the month and day swapped. It matches 12/25/2026 and rejects 25/12/2026. It also matches 02/30/2026, which does not exist.

A regex cannot tell the two formats apart

05/06/2026 matches both patterns. It is 5 June in India and 6 May in the US. A regex can only rule out impossible values, so decide the format from the source of the data, such as the country of the form or the documentation of the file you imported.

YYYY-MM-DD (ISO 8601)

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

This is the format used by HTML date inputs, databases and APIs. It sorts correctly as plain text, which is a good reason to store dates this way. The pattern matches 2026-11-13 and rejects 2026-13-01 (no month 13) and 2026-1-5 (ISO dates use two digits).

Finding dates inside text

For validation you want the whole input to be a date, so you use ^ and $. To find dates inside a sentence, drop the anchors, add word boundaries with \b, and use the g flag:

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

On the text "Invoice 2026-11-13, due 2026-12-01; ref 12026-11-130", this found 2026-11-13 and 2026-12-01 and ignored the reference number, because \b stops a match from starting or ending in the middle of a longer number.

Testing in the Regex Tester

  • Open the Regex Tester.
  • Type the pattern between the two slashes. Do not type the outer slashes yourself.
  • Set the flags in the small box after the pattern. The default is g, which shows every match.
  • Paste sample dates into the test box. Matches are highlighted and listed with their position and captured groups.
  • If you put several dates on separate lines and use a pattern with ^ and $, add the m flag, so the flags box reads gm. Without it, ^ and $ mean the start and end of the whole box, and nothing matches unless the box contains a single date.

    The captured groups are useful: for 25/12/2026 with the DD/MM/YYYY pattern, the tester lists the groups "25" and "12". The year is not in a group because \d{4} has no brackets. Wrap it as (\d{4}) to capture it too.

    Reordering dates with a capture group

    To turn DD/MM/YYYY into YYYY-MM-DD, find (\d{2})\/(\d{2})\/(\d{4}) and replace it with $3-$2-$1. In a test, "15/08/2026 and 26/01/2027" became "2026-08-15 and 2027-01-26". The Find and Replace tool supports this when Use regex is ticked.

    What regex cannot check

    The patterns above accept 31/04/2026 (April has 30 days), 31/02/2026 and 29/02/2026 (2026 is not a leap year). Patterns that handle month lengths and leap years exist, but they are long and easy to get wrong.

    The practical approach is regex for the shape, then code for the calendar. In JavaScript, build the date with Date.UTC(year, month - 1, day) and check that the day and month you get back are the ones you put in. If you ask for 29 February 2026, JavaScript rolls it over to 1 March, so the check fails. A test of this method accepted 29/02/2024 and 30/04/2026 and rejected 29/02/2026 and 31/04/2026.

    Common mistakes

    • Using \d{2} for the day and month. It accepts 99/99/2026.
    • Forgetting the anchors when validating. Without ^ and $, "my date is 25/12/2026!!" passes.
    • Writing [0-31]. Square brackets match a single character, so this means 0, 1, 2 or 3, not 0 to 31.
    • Not escaping the dot. In [\/.-] the dot is literal because it is inside brackets, but outside brackets . matches any character.
    • Testing with only valid dates. Always include a bad month, a bad day and a one-digit value in your samples.

    Related reading

    For other form fields, see regex for Indian mobile numbers, PIN codes and PAN. If a pattern passes in the tester but fails in your program, check why regex works in the tester but not in code.

    Frequently asked questions

    What is the regex for DD/MM/YYYY?

    ^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/\d{4}$ matches a two-digit day from 01 to 31, a two-digit month from 01 to 12 and a four-digit year, separated by slashes.

    What is the regex for YYYY-MM-DD?

    ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$ matches ISO 8601 calendar dates such as 2026-11-13. Remove the ^ and $ and add \b at each end to find these dates inside longer text.

    Can a regex reject 31 February or 29 February in a non-leap year?

    Not in a practical way. The patterns check the shape and the day and month ranges. After a match, check the real date in code, for example by building a Date and confirming the day and month did not roll over.

    How can a regex tell DD/MM/YYYY from MM/DD/YYYY?

    It can only rule out impossible values. 25/12/2026 must be day-first because 25 is not a month, but 05/06/2026 is valid in both formats. Decide the format from where the data came from, not from the regex.

    How do I convert DD/MM/YYYY to YYYY-MM-DD with regex?

    Find (\d{2})\/(\d{2})\/(\d{4}) and replace with $3-$2-$1. For example, 15/08/2026 becomes 2026-08-15.

    Try this tool on Lazyblink

    Put this guide into practice with our free online tool — no signup required.

    Open tool