Regex to Match an ISO 8601 Date-Time
Copy the pattern, paste timestamps, and watch each ISO 8601 date-time light up. Below: what each field means and how the timezone part works.
Regex pattern
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?/g
This matches an ISO 8601 date-time like 2023-01-31T14:30:00, with optional fractional seconds and an optional Z or ±hh:mm offset. It checks the format, not that the date and time are real.
What this pattern matches
It matches an ISO 8601 date-time: YYYY-MM-DD, a literal T, then HH:MM:SS, an optional fractional part, and an optional timezone of Z or ±HH:MM.
Part by part
\d{4}-\d{2}-\d{2} — the calendar date.
T\d{2}:\d{2}:\d{2} — the time separator and clock time.
(?:\.\d+)? — optional milliseconds/microseconds.
(?:Z|[+-]\d{2}:\d{2})? — optional UTC marker or numeric offset.
Note on validity
This pattern checks shape, not calendar validity — it will accept 2026-13-40. To confirm a real date, parse it (e.g. new Date(str)) after the pattern matches.
Use it in your code
import re
text = "Started 2026-09-17T12:30:00Z, ended 2026-09-17T14:05:59.250+02:00. Bad: 2026/09/17."
pattern = r'''\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "Started 2026-09-17T12:30:00Z, ended 2026-09-17T14:05:59.250+02:00. Bad: 2026/09/17.";
const re = new RegExp(String.raw`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?`, "g");
console.log(text.match(re));
FAQ
Does it require the timezone?
No. The Z or ±HH:MM part is optional, so 2026-09-17T12:30:00 matches too.
Does it match milliseconds?
Yes, the (?:\.\d+)? group accepts a dot followed by any number of fractional digits.
Will it reject an impossible date like month 13?
No. It validates format, not calendar range. Parse the string afterwards to confirm it is a real date.