Regex for ISO 8601 Date/Time — Pattern Explained with Examples
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:00Z2024-01-15T14:30:00+05:002024-01-15T14:30:00.123Z2024-01-15T09:00:00-08:002024-06-20T23:59:59.999+00:00
Does NOT Match
2024-01-15 14:30:00— space instead of T separator2024-01-15T14:30— missing seconds01/15/2024T14:30:00Z— wrong date format2024-01-15T24:00:00Z— hour 24 is invalid for ISO 8601 extended time2024-01-15T14:30:00— no timezone2024-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:00Common Pitfalls
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.Z vs offset —
Zrepresents UTC. The alternative+00:00is equivalent but the regex treats them as separate branches. Make sure your parser handles both.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.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_atandupdated_atfields before insertion - Log aggregation — Parsing and standardizing timestamps from distributed systems
FAQ
Related Patterns
- 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