Regex for ISO 8601 Date and DateTime Validation

Regex for ISO 8601 date formats: calendar date (YYYY-MM-DD), datetime with time and timezone, ordinal dates, and week dates. Tested examples and gotchas.

Data Validation

Detailed Explanation

Regex for ISO 8601 Dates

ISO 8601 is the international standard for date and time representation. It supports several profiles, and each has a different regex.

Calendar Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

This validates the structure but not month-day combinations (Feb 30 still passes).

Datetime with Optional Time Zone

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?(Z|[+-]([01]\d|2[0-3]):[0-5]\d)?$

Components:

  • Date part: same as above
  • T separator
  • Time: HH:MM:SS with optional fractional seconds
  • Zone: Z (UTC) or ±HH:MM

Tested Examples

Input Matches?
2024-01-15 yes
2024-12-31T23:59:59Z yes
2024-06-15T10:30:00.123+09:00 yes
2024-1-1 no (single-digit month/day)
2024/01/15 no (slash separator)
2024-13-01 no (month 13)

Ordinal Date (YYYY-DDD)

^\d{4}-(00[1-9]|0[1-9]\d|[12]\d{2}|3[0-5]\d|36[0-6])$

Matches 2024-001 through 2024-366.

Week Date (YYYY-Www-D)

^\d{4}-W(0[1-9]|[1-4]\d|5[0-3])-[1-7]$

Matches 2024-W03-1 (Monday of week 3).

Practical Note

Regex catches malformed input but cannot validate calendar correctness. Always parse with new Date(…) or a library like date-fns and confirm the parsed value round-trips back to the original string.

Use Case

Enforcing ISO timestamp format on REST API request bodies, validating date columns in CSV imports, or filtering log lines that begin with a properly-formatted timestamp.

Try It — Regex Cheat Sheet

Open full tool