Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^([0-9]{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]|60)(\.[0-9]{1,6})?(Z|[+-]([01][0-9]|2[0-3]):([0-5][0-9]))?$

This regex matches a full ISO 8601 date-time string with optional fractional seconds and timezone offset, ensuring valid date and time components.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '2023-12-31T23:59:59Z'
([0-9]{4}) Group 1: Four-digit year (0-9) 2023
- Literal hyphen character -
(0[1-9]|1[0-2]) Group 2: Two-digit month (01-12) 12
- Literal hyphen character -
(0[1-9]|[12][0-9]|3[01]) Group 3: Two-digit day (01-31) 31
T Literal 'T' separator T
([01][0-9]|2[0-3]) Group 4: Two-digit hour (00-23) 23
: Literal colon character :
([0-5][0-9]) Group 5: Two-digit minute (00-59) 59
: Literal colon character :
([0-5][0-9]|60) Group 6: Two-digit second (00-59 or 60 for leap second) 59
(\.[0-9]{1,6})? Group 7: Optional fractional seconds (1-6 digits) .123456
(Z|[+-]([01][0-9]|2[0-3]):([0-5][0-9]))? Group 8: Optional timezone (Z or ±HH:MM) Z or +05:30
$ Anchor: asserts position at end of the string End of '2023-12-31T23:59:59Z'

Matches

  • 2023-12-31T23:59:59Z
  • 2000-01-01T00:00:00+00:00
  • 1999-02-28T12:34:56.123456-05:00
  • 2020-02-29T23:59:60Z

Does not match

  • 2023-13-01T00:00:00Z
  • 2023-12-32T00:00:00Z
  • 2023-12-31T24:00:00Z
  • 2023-12-31T23:60:00Z
  • 2023-12-31T23:59:59
  • 2023-12-31T23:59:59.1234567Z
  • 2023-12-31T23:59:59+24:00

Common pitfalls

  • Leap seconds (60) are allowed but may not be supported by all systems.
  • The regex does not validate the actual calendar date (e.g., February 30 is not caught).
  • Timezone offsets must be in ±HH:MM format; ±H:MM or ±HHMM are not matched.
  • Fractional seconds must be 1-6 digits; 0 digits or 7+ digits are not matched.
  • The 'T' separator is required; spaces or other separators are not matched.

Performance notes

The pattern 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 possible valid ISO 8601 dates?

No. It enforces structural validity and basic range checks (e.g., month 01-12) but does not account for calendar rules like leap years or invalid dates (e.g., February 30).

Why is 60 allowed for seconds?

ISO 8601 permits 60 as a leap second value, though its use is rare and system-dependent.

Can this regex match dates without a timezone?

No. The timezone is optional (due to the outer ?), but if omitted, the regex will not match. To allow timezone-less dates, remove the timezone group and its ? quantifier.