Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])T(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:\.\d{1,3})?(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])$

This regex matches ISO 8601 formatted date-time strings with optional fractional seconds and timezone offsets, starting from the year and ending with the timezone.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string 2023-...
\d{4} Exactly 4 digits (year) 2023
- Literal hyphen separator -
(?:0[1-9]|1[0-2]) Non-capturing group for month: 01-09 or 10-12 01
- Literal hyphen separator -
(?:0[1-9]|[12][0-9]|3[01]) Non-capturing group for day: 01-09, 10-29, or 30-31 15
T Literal 'T' separator between date and time T
(?:[01][0-9]|2[0-3]) Non-capturing group for hour: 00-19 or 20-23 14
: Literal colon separator :
[0-5][0-9] Exactly 2 digits for minutes: 00-59 30
: Literal colon separator :
[0-5][0-9] Exactly 2 digits for seconds: 00-59 45
(?:\.\d{1,3})? Optional non-capturing group for fractional seconds: .1 to .999 .123
(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9]) Non-capturing group for timezone: 'Z' or ±HH:MM Z or +05:30
$ Anchor: asserts position at end of the string ...30Z

Matches

  • 2023-01-15T14:30:45Z
  • 2023-12-31T23:59:59.999+05:30
  • 1999-02-28T00:00:00.1Z
  • 2000-01-01T00:00:00-12:00

Does not match

  • 2023-13-01T00:00:00Z
  • 2023-01-32T00:00:00Z
  • 2023-01-01T24:00:00Z
  • 2023-01-01T12:60:00Z
  • 2023-01-01T12:00:60Z
  • 2023-01-01T12:00:00.1234Z
  • 2023-01-01T12:00:00+24:00

Common pitfalls

  • Invalid months (e.g., 13) or days (e.g., 32) are not matched
  • Timezone offsets must be in ±HH:MM format; ±HHMM is not supported
  • Fractional seconds must be 1-3 digits; more digits are not matched
  • Hour must be 00-23; 24 is invalid
  • Seconds must be 00-59; 60 is invalid

Performance notes

The regex is linear in complexity with no nested quantifiers, so catastrophic backtracking is not a concern. Anchors (^ and $) ensure full-string matching, which is efficient.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this regex validate all ISO 8601 date-time strings?

No. It validates a subset: dates with 4-digit years, optional fractional seconds (1-3 digits), and timezone offsets in ±HH:MM format. It does not support other ISO 8601 features like week dates or ordinal dates.

Why is the month group (?:0[1-9]|1[0-2]) used instead of [01][0-9]?

Using [01][0-9] would match invalid months like 00 or 19. The explicit alternation ensures only valid months (01-12) are matched.

Can this regex match times with no timezone?

No. The timezone part (?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9]) is required. To make it optional, wrap it in another non-capturing group with a ? quantifier.