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 or 'Z' for UTC, starting from the year and ending with the timezone.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string 2023-01-01T12:34:56Z
\d{4} Exactly 4 digits (year) 2023
- Literal hyphen character -
\d{1,2} 1 or 2 digits (month) 01
- Literal hyphen character -
\d{1,2} 1 or 2 digits (day) 01
T Literal 'T' character separating date and time T
\d{1,2} 1 or 2 digits (hour in 24-hour format) 12
: Literal colon character :
\d{2} Exactly 2 digits (minutes) 34
: Literal colon character :
\d{2} Exactly 2 digits (seconds) 56
(?:\.\d{1,6})? Non-capturing group: optional fractional seconds (1 to 6 digits) preceded by a dot .123456
(?:Z|[+-]\d{2}:\d{2}) Non-capturing group: either 'Z' for UTC or timezone offset in ±HH:MM format Z
$ Anchor: asserts position at end of the string 2023-01-01T12:34:56Z

Matches

  • 2023-01-01T12:34:56Z
  • 2023-12-31T23:59:59.999999+05:30
  • 1999-02-28T00:00:00-08:00
  • 2000-01-01T00:00:00.1Z

Does not match

  • 2023/01/01T12:34:56Z
  • 2023-1-1T12:34:56Z
  • 2023-01-01 12:34:56Z
  • 2023-01-01T12:34:56
  • 2023-01-01T12:34:56.1234567Z
  • 2023-01-01T25:00:00Z
  • 2023-01-01T12:60:00Z

Common pitfalls

  • Forgetting the 'T' separator between date and time
  • Using single-digit months or days without leading zeros
  • Including more than 6 fractional seconds digits
  • Omitting the timezone entirely
  • Using invalid timezone offsets (e.g., +25:00)
  • Assuming this matches all ISO 8601 formats (e.g., it does not match formats with space instead of 'T')

Performance notes

The pattern is linear and does not contain nested quantifiers, so catastrophic backtracking is not a concern. Anchors (^ and $) ensure the entire string is matched, improving efficiency. For very long strings, the anchors prevent partial matches.

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

No. It matches a subset of ISO 8601 datetime strings with specific formats: YYYY-MM-DDTHH:MM:SS[.ffffff][Z|±HH:MM]. It does not match formats with space separators, different timezone representations, or extended formats like YYYYMMDDTHHMMSS.

Why is the fractional seconds part optional?

The (?: \.\d{1,6})? group is optional to accommodate datetime strings without fractional seconds, which is common in many applications.

Can this pattern match timezones like +0530 without a colon?

No. The pattern strictly requires the timezone offset to be in the format ±HH:MM (e.g., +05:30 or -08:00). Timezones without a colon (e.g., +0530) will not match.

Is this pattern suitable for validating user input in a web form?

It can be used for basic validation, but additional checks (e.g., valid date ranges, timezone validity) should be performed server-side for robustness.