Regex
regex
December 24, 20224 min read

Regex for dd-mm-yyyy

A date is a specific day or time period, often given as a combination of a month, date, and year. In this article let's understand how we can create a regex for date dd-mm-yyyy, dd/mm/yyyy and dd.mm.yyyy and how regex can be matched for a given date.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of a dd-mm-yyyy or dd/mm/yyyy or dd.mm.yyyy

The date should have the following criteria and structure-

  • Date can be d or dd - where d is a digit
  • Month can be m or mm - where m is a digit
  • Year can be yy or yyyy - where y is a digit
  • Supported separators are - or / or .
  • Supported date formats are dd-mm-yyyy, dd/mm/yyyy, dd.mm.yyyy
  • Leap year is supported

Regex for checking if dd-mm-yyyy is valid or not

Regular Expression for dd-mm-yyyy or dd/mm/yyyy or dd.mm.yyyy with Leap year support is-

/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/gm

Test string examples for the above regex-

Input StringMatch Output
33-43-12does not match
1-2-22matches
332-122-21does not match
02-12-2022matches
2-11-22matches
2/11/22matches
2.11.22matches

Here is a detailed explanation of the above regex-

/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/gm

1st Alternative ^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$
^ asserts position at start of a line
Non-capturing group (?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))
1st Alternative (?:31(\/|-|\.)(?:0?[13578]|1[02]))\1
Non-capturing group (?:31(\/|-|\.)(?:0?[13578]|1[02]))
31 matches the characters 31 literally (case sensitive)
1st Capturing Group (\/|-|\.)
1st Alternative \/
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
2nd Alternative -
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
3rd Alternative \.
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
Non-capturing group (?:0?[13578]|1[02])
1st Alternative 0?[13578]
0 matches the character 0 with index 4810 (3016 or 608) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [13578]
13578 matches a single character in the list 13578 (case sensitive)
2nd Alternative 1[02]
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
Match a single character present in the list below [02]
02 matches a single character in the list 02 (case sensitive)
\1 matches the same text as most recently matched by the 1st capturing group
2nd Alternative (?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2)
Non-capturing group (?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2)
Non-capturing group (?:29|30)
1st Alternative 29
29 matches the characters 29 literally (case sensitive)
2nd Alternative 30
30 matches the characters 30 literally (case sensitive)
2nd Capturing Group (\/|-|\.)
1st Alternative \/
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
2nd Alternative -
3rd Alternative \.
Non-capturing group (?:0?[13-9]|1[0-2])
1st Alternative 0?[13-9]
2nd Alternative 1[0-2]
\2 matches the same text as most recently matched by the 2nd capturing group
Non-capturing group (?:(?:1[6-9]|[2-9]\d)?\d{2})
Non-capturing group (?:1[6-9]|[2-9]\d)?
\d matches a digit (equivalent to [0-9])
$ asserts position at the end of a line
2nd Alternative ^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$
^ asserts position at start of a line
Non-capturing group (?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))
$ asserts position at the end of a line
3rd Alternative ^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
^ asserts position at start of a line
Non-capturing group (?:0?[1-9]|1\d|2[0-8])
4th Capturing Group (\/|-|\.)
Non-capturing group (?:(?:0?[1-9])|(?:1[0-2]))
\4 matches the same text as most recently matched by the 4th capturing group
Non-capturing group (?:(?:1[6-9]|[2-9]\d)?\d{2})
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to check if the string is a valid dd-mm-yyyy or not.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
January 13, 20232 min read
Next Article
December 19, 20225 min read