Skip to content

Regex for ISO 8601 DateTime

This regex matches ISO 8601 datetime strings including date, time, optional fractional seconds, and optional timezone offset. ISO 8601 is the standard format used by JSON, REST APIs, databases, and most programming languages. The pattern validates the full datetime structure including the T separator and Z or +/-HH:MM timezone designator.

Pattern
^\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)?$
Test this pattern in the Regex Tester →

What is the regex pattern for ISO 8601 DateTime?

The regex pattern for ISO 8601 DateTime is ^\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)?$. This regex matches ISO 8601 datetime strings including date, time, optional fractional seconds, and optional timezone offset. ISO 8601 is the standard format used by JSON, REST APIs, databases, and most programming languages. The pattern validates the full datetime structure including the T separator and Z or +/-HH:MM timezone designator. This pattern is commonly used for api response parsing and json date fields.

Test Examples

Match
2024-01-15T14:30:00Z
Matches: 2024-01-15T14:30:00Z
Match
2023-12-31T23:59:59.999+05:30
Matches: 2023-12-31T23:59:59.999+05:30
No Match
2024-01-15 14:30:00

Common Uses

Variations

Without timezone

^\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)$

Local datetime without timezone info

UTC only

^\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)Z$

Only matches UTC (Z) timestamps

Space separator

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

Uses space instead of T (common in SQL)

Frequently Asked Questions

What does the T between date and time mean?

The T is a literal character that separates the date and time components in ISO 8601 format. It ensures unambiguous parsing and is required by the standard, though some systems accept a space instead.

What does the Z at the end mean?

Z stands for 'Zulu time' and indicates UTC (Coordinated Universal Time). It means the timestamp has no timezone offset. A timestamp without Z or an offset is considered a local time with no timezone information.

Why use ISO 8601 for APIs?

ISO 8601 is unambiguous, sorts correctly as a string, includes timezone information, and is supported by every major programming language. It avoids the locale-specific ambiguity of formats like MM/DD/YYYY vs DD/MM/YYYY.

Related Patterns

Date (YYYY-MM-DD)

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

24-Hour Time

^([01]\d|2[0-3]):([0-5]\d)$

Date (MM/DD/YYYY)

^(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])/\...

Related Reading

Regex Cheat Sheet with Examples for Developers → URL Encoding Special Characters →