Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^\d{4}-\d{2}-\d{2}T\d{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 or 'Z' for UTC.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
\d{4} Exactly 4 digits (0-9) 2023
- Literal hyphen character -
\d{2} Exactly 2 digits (0-9) 12
- Literal hyphen character -
\d{2} Exactly 2 digits (0-9) 31
T Literal 'T' character separating date and time T
\d{2} Exactly 2 digits (0-9) for hours 23
: Literal colon character :
\d{2} Exactly 2 digits (0-9) for minutes 59
: Literal colon character :
\d{2} Exactly 2 digits (0-9) for seconds 59
(?:\.\d{1,6})? Non-capturing group: optional fractional seconds (1 to 6 digits) .123456 or .1
(?:Z|[+-]\d{2}:\d{2}) Non-capturing group: either 'Z' for UTC or timezone offset in ±HH:MM format Z or +05:30 or -08:00
$ Anchor: asserts position at end of the string End of string

Matches

  • 2023-12-31T23:59:59Z
  • 2023-12-31T23:59:59.123456Z
  • 2023-12-31T23:59:59+05:30
  • 2023-12-31T23:59:59-08:00
  • 1970-01-01T00:00:00Z

Does not match

  • 2023-12-31T23:59:59
  • 2023-12-31T23:59:59.1234567Z
  • 2023-12-31T24:00:00Z
  • 2023-12-31T23:59:59+25:00
  • 2023-12-31T23:59:59.123456+05:30

Common pitfalls

  • Omitting the 'T' separator between date and time
  • Using more than 6 fractional digits
  • Incorrect timezone offset format (e.g., +5:30 instead of +05:30)
  • Forgetting to escape the hyphen in character classes (though not needed here)
  • Assuming this matches all valid ISO 8601 datetimes (e.g., it does not match '2023-12-31 23:59:59Z')

Performance notes

The pattern is linear and does not contain nested quantifiers or alternations that could lead to catastrophic backtracking. Anchors (^ and $) ensure efficient matching by preventing partial matches. No significant performance concerns.

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 all ISO 8601 datetime strings?

No. It only matches the specific format with 4-digit year, 2-digit month/day/hour/minute/second, optional fractional seconds up to 6 digits, and either 'Z' or ±HH:MM timezone offset. It does not cover all ISO 8601 variants (e.g., week dates, ordinal dates, or reduced accuracy).

Can this pattern match datetimes with no timezone?

No. The pattern requires either 'Z' or a timezone offset (±HH:MM) at the end. To make timezone optional, modify the last group to (?:Z|[+-]\d{2}:\d{2})?.

Why use non-capturing groups (?:...) instead of capturing groups (...)?

Non-capturing groups are used here for efficiency since the groups are only for grouping and not for capturing substrings. Capturing groups would unnecessarily store matched text in memory.