Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression timezone

I need a regular expression for valid time zone, tried the following one.
But I'm not sure about it.
Please help me to find out anything wrong in the following regular expression.

Edited:

Here colon and minutes are optional. how can i change it to mandatory.
If there is no minutes user should input 00 (+05:00). Please help me to solve this issue.

var chkzone = "+05:30"
if(chkzone .match(/^(Z|[+-](?:2[0-3]|[01]?[0-9])(?::?(?:[0-5]?[0-9]))?)$/))
{
    alert('works out');
}
else
{
    alert("Time zone wrong")
}
like image 753
Dibish Avatar asked Mar 24 '14 12:03

Dibish


People also ask

How to check time in 24-hour format using regular expression?

Create a regular expression to check time in 24-hour format as mentioned below: ( represents the start of the group. [01]? [0-9] represents the time starts with 0-9, 1-9, 00-09, 10-19. | represents or. 2 [0-3] represents the time starts with 20-23. ) represents the end of the group. : represents the time should be followed by a colon (:).

How do I match a Julian date with a regular expression?

A regular expression to match a Julian date in the format YYDDD. /^ ( [0-9] {2}) (00 [1-9]|0 [1-9] [0-9]| [1-2] [0-9] [0-9]|3 [0-5] [0-9]|36 [0-6])$/ A regular expression to match a valid date in the format YYYY-MM-DD. Date, Month, Leap Year validation is included.

Which regular expression can be used to validate the month input?

A regular expression for month that can be used to validate month input. A Regular Expression to match and validate Credit Card Expiration Date in MM/YY format. A regular expression to match an EU date format: DD-MM-YYYY.

What is the time format for out of range text?

“1:00”, “2:00”, “13:01”, 3. “23:59″,”15:00” 4. “00:00″,”0:00” Time format doesn’t match: 1. “24:00” – hour is out of range [0-23] 2. “12:60” – minute is out of range [00-59] 3. “0:0” – invalid format for minute, at least 2 digits 4. “13:1” – invalid format for minute, at least 2 digits 5. “101:00” – hour is out of range [0-23]


1 Answers

The following regex matches timezones with mandatory double digit hours/minutes, or the letter Z:

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

Explanation:

^            # Start of string
(?:          # Match the following non-capturing group:
 Z           # Either a literal Z
|            # or
 [+-]        # a plus or minus sign
 (?:         # followed by this non-capturing group:
  2[0-3]     # Either a number between 20 and 23
 |           # or
  [01][0-9]  # a number between 00 and 19
 )           # End of inner non-capturing group
 :           # Match a literal colon
 [0-5][0-9]  # Match a number between 00 and 59
)            # End of outer non-capturing group
$            # End of string

See it live on regex101.com.

like image 121
Tim Pietzcker Avatar answered Sep 22 '22 15:09

Tim Pietzcker