Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}:\d{2}(?:\.\d{1,6})?(?:Z|[+-]\d{2}:?\d{2})?$

Matches ISO 8601 datetime strings with optional fractional seconds and timezone offset, from start to end of string.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '2023-05-15T14:30:45Z'
\d{4} Exactly 4 digits (year) 2023
- Literal hyphen character -
\d{1,2} 1 or 2 digits (month) 05
- Literal hyphen character -
\d{1,2} 1 or 2 digits (day) 15
T Literal 'T' character separating date and time T
\d{1,2} 1 or 2 digits (hour in 24-hour format) 14
: Literal colon character :
\d{2} Exactly 2 digits (minutes) 30
: Literal colon character :
\d{2} Exactly 2 digits (seconds) 45
(?:\.\d{1,6})? Non-capturing group: optional fractional seconds (1 to 6 digits) preceded by a dot .123456 or .1
(?:Z|[+-]\d{2}:?\d{2})? Non-capturing group: optional timezone indicator (Z for UTC or ±HH:MM/HHMM) Z or +02:00 or -0500
$ Anchor: asserts position at end of the string End of '2023-05-15T14:30:45Z'

Matches

  • 2023-05-15T14:30:45Z
  • 2023-5-5T9:5:5.123456+02:00
  • 1999-12-31T23:59:59.999999-0500
  • 0001-01-01T00:00:00Z

Does not match

  • 2023/05/15T14:30:45Z
  • 2023-05-15 14:30:45Z
  • 2023-05-15T14:30:45
  • 2023-05-15T14:30:45.1234567Z
  • 2023-05-15T25:00:00Z
  • 2023-05-15T14:30:45+2:00

Common pitfalls

  • Timezone offset without colon (e.g., +0200) is not matched due to the optional colon in the pattern
  • Month or day with leading zero is required (e.g., '5' is valid but '05' is also valid)
  • Fractional seconds must be 1 to 6 digits; longer sequences are not matched
  • The pattern does not validate logical date/time values (e.g., '2023-13-01' is matched but invalid)
  • The pattern allows single-digit hours, minutes, or seconds (e.g., '9:5:5'), which may not be desired in some contexts

Performance notes

The pattern is linear and does not contain nested quantifiers or alternations that could cause catastrophic backtracking. Anchors (^ and $) ensure the entire string is checked efficiently. Performance is generally good for typical ISO 8601 strings.

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 pattern validate the actual date/time values (e.g., month 13)?

No, this pattern only checks the format. It will match strings like '2023-13-01T00:00:00Z' even though the date is invalid.

Why doesn't the pattern match timezone offsets like '+0200' without a colon?

The pattern includes an optional colon (:?) after the hours in the timezone offset, so '+0200' is not matched. To match both formats, the pattern would need to be adjusted, e.g., (?::?\d{2})?

Can this pattern match times with single-digit hours, minutes, or seconds?

Yes, the pattern allows single-digit hours, minutes, or seconds (e.g., '2023-05-15T9:5:5Z'). If strict two-digit formatting is required, the quantifiers should be adjusted to \d{2}.

Is the fractional seconds part optional?

Yes, the fractional seconds part is entirely optional due to the outer non-capturing group being quantified with ?.

Does this pattern work for all ISO 8601 datetime formats?

No, this pattern is a subset of ISO 8601. It does not match formats like '2023-05-15T14:30:45+0200' (without colon in timezone) or '20230515T143045Z' (basic format).