Date/Time
Timezone Offset Regex
Timestamps often carry a timezone designator: Z for UTC, or a signed offset such as +05:00, -08:00 or +0530. This pattern accepts Z or a plus or minus sign followed by a two-digit hour from 00 to 23 and a two-digit minute, with the colon optional. It is useful when timezone offsets are stored in a separate field, in configuration files and when validating parts of a parsed timestamp. The honest limit is that it accepts offsets that no real timezone uses, such as +23:59, and it does not understand timezone names like Asia/Dubai.
Pattern (JavaScript)
/^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$/Starts at the start of the line/string, then (the character "Z" OR one of the characters +-, then (one of the characters 01, then a digit OR the character "2", then one of the characters 0-3), then the character ":", optionally (zero or one time), then one of the characters 0-5, then a digit), then the end of the line/string.
Examples
Matches
- ✓ Z
- ✓ +05:00
- ✓ -0800
- ✓ +05:30
Does not match
- ✗ +24:00
- ✗ +5:00
- ✗ GMT
Try it
0 matches (JavaScript engine)
Z
+05:00
-0800
+05:30
+24:00
+5:00
GMTCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$/g;
const text = "Z";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$')
text = 'Z'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$/';
$text = 'Z';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$");
String text = "Z";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$`)
text := "Z"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$");
var text = "Z";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$").unwrap();
let text = "Z";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:Z|[+-](?:[01][0-9]|2[0-3]):?[0-5][0-9])$/
text = 'Z'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is +5:00 rejected?
- ISO 8601 requires two-digit hours in offsets, so +05:00 is the correct form. Pad single-digit hours with a leading zero before testing.
- What is the real range of timezone offsets?
- Real offsets currently range from -12:00 to +14:00. The pattern is looser for simplicity; tighten it in code if you need only offsets that are in use.
- How do I handle timezone names like Europe/London?
- Names come from the IANA time zone database and should be checked against that list, for example with Intl.supportedValuesOf in JavaScript. A regex cannot know which names exist.