Skip to content

Regex for ISO 8601 Date/Time — Pattern Explained with Examples

DodaTech Updated 2026-06-20 2 min read

In this tutorial, you'll learn about Regex for ISO 8601 Date/Time. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

This regex validates ISO 8601 date-time strings in the extended format: YYYY-MM-DDTHH:mm:ss with an optional timezone offset (Z or ±HH:mm). This format is the international standard for date and time representation and is widely used in JSON APIs, database timestamps, and data interchange formats like JSON and XML.

The Pattern

/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/

Pattern Breakdown

Part Meaning
^ Start of string
\d{4}-\d{2}-\d{2} Date in YYYY-MM-DD format
T Literal T separator (date/time)
\d{2}:\d{2}:\d{2} Time in HH:mm:ss format
(\.\d+)? Optional fractional seconds
(Z\|[+-]\d{2}:\d{2}) Timezone: Zulu (UTC) or ±HH:mm offset
$ End of string

Matches

  • 2024-01-15T14:30:00Z
  • 2024-01-15T14:30:00+05:00
  • 2024-01-15T14:30:00.123Z
  • 2024-01-15T09:00:00-08:00
  • 2024-06-20T23:59:59.999+00:00

Does NOT Match

  • 2024-01-15 14:30:00 — space instead of T separator
  • 2024-01-15T14:30 — missing seconds
  • 01/15/2024T14:30:00Z — wrong date format
  • 2024-01-15T24:00:00Z — hour 24 is invalid for ISO 8601 extended time
  • 2024-01-15T14:30:00 — no timezone
  • 2024-01-15T14:30:00+5:00 — timezone offset missing leading zero on hour

Language Examples

JavaScript

const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;

console.log(isoRegex.test('2024-01-15T14:30:00Z'));         // true
console.log(isoRegex.test('2024-01-15T14:30:00.123+05:00')); // true
console.log(isoRegex.test('2024-01-15 14:30:00Z'));          // false

// Parse ISO string into Date object
const date = new Date('2024-01-15T14:30:00Z');
console.log(date.toISOString());  // 2024-01-15T14:30:00.000Z

Python

import re
from datetime import datetime

iso_regex = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$'

print(bool(re.match(iso_regex, '2024-01-15T14:30:00Z')))              # True
print(bool(re.match(iso_regex, '2024-01-15T14:30:00.123+05:00')))     # True
print(bool(re.match(iso_regex, '2024-01-15 14:30:00Z')))              # False

# Parse ISO string (Python 3.7+)
dt = datetime.fromisoformat('2024-01-15T14:30:00+05:00'.replace('Z', '+00:00'))
print(dt.isoformat())  # 2024-01-15T14:30:00+05:00

Common Pitfalls

  1. T separator required — ISO 8601 allows a space in some profiles (like RFC 3339), but the strict standard requires T. Decide which variant your application follows.

  2. Z vs offsetZ represents UTC. The alternative +00:00 is equivalent but the regex treats them as separate branches. Make sure your parser handles both.

  3. Fractional seconds are optional — The regex allows decimals (.123) but does not enforce a specific precision. Some systems require exactly 3 digits (milliseconds); adjust the quantifier accordingly.

  4. Timezone mandatory in this pattern — This regex requires a timezone. If your system allows naive datetimes (no timezone), make the timezone group optional with ?(Z|[+-]\d{2}:\d{2})?$.

Real-World Use Cases

  • REST API validation — Ensuring request bodies contain properly formatted timestamps
  • Database timestamps — Validating created_at and updated_at fields before insertion
  • Log aggregationParsing and standardizing timestamps from Distributed Systems

FAQ

{{< faq-item "How do I also accept a space instead of T?">}}

Replace T with [T ] in the pattern: /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/. This is common in RFC 3339 profiles. {{< /faq-item>}}

{{< faq-item "Does this validate February 29 or leap seconds?">}}

No. Like other date patterns, this regex only validates format, not calendar correctness or leap second handling (e.g., 23:59:60). Use a dedicated date-time library for semantic validation. {{< /faq-item>}}

  • Regex for Date (YYYY-MM-DD)
  • Regex for Time (HH:MM)
  • Regex for Date (MM/DD/YYYY)

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro