Regex
regex
January 10, 20233 min read

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

Time is a concept that is used to describe the duration of events or the progression of existence. It is often measured in units such as seconds, minutes, hours, days, and years. A time zone is a geographic region that has adopted the same standard time for legal, commercial, and social purposes. In this article let's understand how we can create a regex for timezone and how regex can be matched for a given timezone.

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 timezone

The time should have the following criteria and structure-

  • It should start with either + or -
  • It will be followed by two digits for hours
  • It will be followed by :
  • It will be followed by two digits for minutes
  • Other special characters are not allowed

Regex for checking if timezone is valid or not

Regular Expression for timezone is-

/^(?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])$/gm

Test string examples for the above regex-

Input StringMatch Output
02:20does not match
+33:11does not match
+05:30matches
-10:00matches
+00:00matches

Here is a detailed explanation of the above regex-

/^(?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])$/gm

^ asserts position at start of the string
Non-capturing group (?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])
1st Alternative Z
Z matches the character Z with index 9010 (5A16 or 1328) literally (case sensitive)
2nd Alternative [+-](?:2[0-3]|[01][0-9]):[0-5][0-9]
Match a single character present in the list below [+-]
+- matches a single character in the list +- (case sensitive)
Non-capturing group (?:2[0-3]|[01][0-9])
1st Alternative 2[0-3]
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below [0-3]
0-3 matches a single character in the range between 0 (index 48) and 3 (index 51) (case sensitive)
2nd Alternative [01][0-9]
Match a single character present in the list below [01]
01 matches a single character in the list 01 (case sensitive)
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 sensitive)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below [0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
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 sensitive)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

Hope this article was useful to check if the string is a valid timezone 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
Next Article
January 10, 20233 min read