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-10-05T14:30:00Z'
\d{4} Exactly 4 digits (year) 2023
- Literal hyphen character -
\d{1,2} 1 or 2 digits (month) 10
- Literal hyphen character -
\d{1,2} 1 or 2 digits (day) 05
T Literal 'T' character (ISO 8601 separator) 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) 00
(?:\.\d{1,6})? Non-capturing group: optional fractional seconds (1-6 digits after dot) .123456
(?:Z|[+-]\d{2}:\d{2})? Non-capturing group: optional timezone (Z for UTC or ±HH:MM offset) Z or +02:00
$ Anchor: asserts position at end of the string End of '2023-10-05T14:30:00Z'

Matches

  • 2023-10-05T14:30:00Z
  • 2023-1-5T1:3:0Z
  • 2023-12-31T23:59:59.999999+05:30
  • 2000-01-01T00:00:00-12:00
  • 1999-12-31T23:59:59.123456

Does not match

  • 2023/10/05T14:30:00Z
  • 2023-10-05 14:30:00Z
  • 2023-10-05T25:00:00Z
  • 2023-10-05T14:30:00
  • 2023-10-05T14:30:00.1234567Z
  • 2023-10-05T14:30:00+25:00

Common pitfalls

  • Forgetting the ^ and $ anchors may cause partial matches (e.g., matching '2023-10-05T14:30:00Zextra').
  • Assuming the pattern validates actual date/time values (e.g., '2023-13-01' would match but is invalid).
  • Overlooking that the timezone offset must be exactly ±HH:MM (e.g., '+0530' without colon fails).
  • Not accounting for optional fractional seconds (may expect them to always be present).

Performance notes

The pattern is linear and has no quantifier nesting, so catastrophic backtracking is not a concern. Anchors ensure full-string matching, which is efficient. For large datasets, pre-filter strings that don't start with 4 digits to improve performance.

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 actual dates (e.g., February 30)?

No. The regex only checks the format, not the validity of the date/time values. Additional validation is required for business logic.

Can the timezone offset be omitted entirely?

Yes. The timezone group is optional (due to the ? quantifier), so strings like '2023-10-05T14:30:00' are valid matches.

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

Non-capturing groups improve performance by avoiding unnecessary capture overhead, as the groups are only used for grouping, not extraction.

Does this work for all ISO 8601 datetime formats?

No. This pattern covers a subset: dates with optional fractional seconds and timezone offsets in ±HH:MM format. It does not support other ISO 8601 variants like week dates or ordinal dates.