Regex for ISO 8601 date and time

Use /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/ to check that a whole string is a valid for ISO 8601 date and time. Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/
Open in Regex Tester
InputResult
2026-10-30T14:30:00Z✓ Match
2026-10-30T14:30:00.123+05:30✓ Match
2026-10-30T14:30Z✓ Match
2026-10-30 14:30:00✗ No match
2026-10-30T14:30:00✗ No match
2026-10-30T14:30:00+0530✗ No match

How the pattern works

PartMeaning
\d{4}-\d{2}-\d{2}The date
TThe literal T separator
\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?Hours and minutes, optional seconds and fractions
(?:Z|[+-]\d{2}:\d{2})Z for UTC, or an offset such as +05:30

Code examples

JavaScript

const re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/;
re.test(value); // true or false

Python

import re

bool(re.fullmatch(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})', value))

PHP

preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/', $value) === 1;

Notes and limitations

This requires a time zone, which avoids ambiguous timestamps. new Date().toISOString() in JavaScript always produces a matching string. Convert timestamps with the Unix Timestamp Converter.

Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool

Frequently asked questions

What is the regex for ISO 8601 date and time?
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: 2026-10-30T14:30:00Z, 2026-10-30T14:30:00.123+05:30, 2026-10-30T14:30Z. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.