Regex
regex
December 24, 20223 min read

Regex for yyyy-mm-dd hh:mm:ss

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 yyyymmdd 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 yyyymmdd

The date should have the following criteria and structure-

  • Date is dd - where d is a digit
  • Month is mm - where m is a digit
  • Year is yyyy - where y is a digit
  • Supported date format is yyyymmdd

Simple regex for checking if yyyy-mm-dd is valid or not

/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/gm

Test string examples for the above regex-

Input StringMatch Output
33-43-12does not match
2022-12-12matches
332-122-21does not match
2992-12-12matches

Here is a detailed explanation of the above regex-

/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/gm

Match a single character present in the list below [0-9]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
1st Capturing Group (0[1-9]|1[0-2])
1st Alternative 0[1-9]
0 matches the character 0 with index 4810 (3016 or 608) literally (case insensitive)
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case insensitive)
2nd Alternative 1[0-2]
1 matches the character 1 with index 4910 (3116 or 618) literally (case insensitive)
Match a single character present in the list below [0-2]
0-2 matches a single character in the range between 0 (index 48) and 2 (index 50) (case insensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
2nd Capturing Group (0[1-9]|[1-2][0-9]|3[0-1])
1st Alternative 0[1-9]
0 matches the character 0 with index 4810 (3016 or 608) literally (case insensitive)
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case insensitive)
2nd Alternative [1-2][0-9]
Match a single character present in the list below [1-2]
1-2 matches a single character in the range between 1 (index 49) and 2 (index 50) (case insensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
3rd Alternative 3[0-1]
3 matches the character 3 with index 5110 (3316 or 638) literally (case insensitive)
Match a single character present in the list below [0-1]
0-1 matches a single character in the range between 0 (index 48) and 1 (index 49) (case insensitive)
Global pattern flags
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches (don't return after first match)

Hope this article was useful to check if the string is a valid yyyymmdd name 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
December 12, 20226 min read
Next Article
December 24, 20227 min read