T
ToolPrime

Regex for ISO 8601 Datetime

Validates ISO 8601 datetime strings like 2024-03-28T15:30:00Z with optional timezone offset.

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(Z|[+-]([01]\d|2[0-3]):[0-5]\d)?$/

Live Tester

Enter a string to test

Examples

2024-03-28T15:30:00Z
2023-12-31T23:59:59+05:30
2000-01-01T00:00:00-08:00
2024-03-28
2024-13-28T15:30:00Z
2024-03-28T25:30:00Z

Pattern Breakdown

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) — date portion YYYY-MM-DD

T([01]\d|2[0-3]):[0-5]\d:[0-5]\d — time portion HH:MM:SS

(Z|[+-]([01]\d|2[0-3]):[0-5]\d)?$ — optional Z or +/-HH:MM timezone

Code Snippets

Javascript

const regex = /^\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|[+-]([01]\d|2[0-3]):[0-5]\d)?$/;
regex.test("2024-03-28T15:30:00Z"); // true

Python

import re
pattern = r"^\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|[+-]([01]\d|2[0-3]):[0-5]\d)?$"
bool(re.match(pattern, "2024-03-28T15:30:00Z"))  # True

Php

$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(Z|[+-]([01]\d|2[0-3]):[0-5]\d)?$/';
preg_match($pattern, "2024-03-28T15:30:00Z"); // 1

Related Patterns

Frequently Asked Questions

What is the regex 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(Z|[+-]([01]\d|2[0-3]):[0-5]\d)?$/. Validates ISO 8601 datetime strings like 2024-03-28T15:30:00Z with optional timezone offset.
How do I use the ISO 8601 Datetime regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the ISO 8601 Datetime pattern match?
It matches strings like 2024-03-28T15:30:00Z; it rejects strings like 2024-03-28. See the Examples section above for the full list of matching and non-matching cases.

Related Tools