Regex to Match ISO 8601 DateTime

Validate ISO 8601 datetime strings with this regex. Matches timestamps with timezone offsets like 2024-01-15T10:30:00Z. Free tool.

Regular Expression

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

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
\dMatches any digit (0-9)
{4}Matches exactly 4 times
-Matches the literal character '-'
(?:Start of non-capturing group
0Matches the literal character '0'
[1-9]Character class — matches any one of: 1-9
|Alternation — matches the expression before OR after the pipe
1Matches the literal character '1'
[0-2]Character class — matches any one of: 0-2
)End of group
-Matches the literal character '-'
(?:Start of non-capturing group
0Matches the literal character '0'
[1-9]Character class — matches any one of: 1-9
|Alternation — matches the expression before OR after the pipe
[12]Character class — matches any one of: 12
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
3Matches the literal character '3'
[01]Character class — matches any one of: 01
)End of group
TMatches the literal character 'T'
(?:Start of non-capturing group
[01]Character class — matches any one of: 01
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
2Matches the literal character '2'
[0-3]Character class — matches any one of: 0-3
)End of group
:Matches the literal character ':'
[0-5]Character class — matches any one of: 0-5
\dMatches any digit (0-9)
:Matches the literal character ':'
[0-5]Character class — matches any one of: 0-5
\dMatches any digit (0-9)
(?:Start of non-capturing group
\.Matches a literal dot
\dMatches any digit (0-9)
{1,3}Matches between 1 and 3 times
)End of group
?Makes the preceding element optional (zero or one times)
(?:Start of non-capturing group
ZMatches the literal character 'Z'
|Alternation — matches the expression before OR after the pipe
[+-]Character class — matches any one of: +-
(?:Start of non-capturing group
[01]Character class — matches any one of: 01
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
2Matches the literal character '2'
[0-3]Character class — matches any one of: 0-3
)End of group
:Matches the literal character ':'
[0-5]Character class — matches any one of: 0-5
\dMatches any digit (0-9)
)End of group
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates ISO 8601 datetime strings including timezone information. Here is the token-by-token breakdown:

\d{4} — Matches a four-digit year.

-(?:0[1-9]|1[0-2]) — Matches a hyphen followed by a valid month (01-12).

-(?:0[1-9]|[12]\d|3[01]) — Matches a hyphen followed by a valid day (01-31).

T — Matches the literal 'T' separator between date and time, as required by ISO 8601.

(?:[01]\d|2[0-3]) — Matches valid hours (00-23). [01]\d covers 00-19 and 2[0-3] covers 20-23.

:[0-5]\d — Matches a colon followed by valid minutes (00-59).

:[0-5]\d — Matches a colon followed by valid seconds (00-59).

(?:.\d{1,3})? — An optional group matching fractional seconds (milliseconds), one to three digits after a decimal point.

(?:Z|+-:[0-5]\d) — Matches the timezone designator. Either 'Z' for UTC or a +/- offset with hours and minutes. For example, +05:30 or -08:00.

This pattern is essential for validating API responses, log timestamps, and database datetime fields. The ISO 8601 format is the international standard for date and time representation and is used extensively in JSON APIs, XML documents, and programming language standard libraries.

Example Test Strings

InputExpected
2024-01-15T10:30:00ZMatch
2023-12-31T23:59:59.999+05:30Match
2024-01-15T25:00:00ZNo Match
2024-01-15 10:30:00No Match
2024-06-15T08:00:00-08:00Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 132 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →