ISO 8601 DateTime Regex

^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$

Match full ISO 8601 datetime with timezone. Includes optional milliseconds and timezone offset.

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$:

  • ^ - Start of string
  • $ - End of string
  • \d - Any digit (0-9)
  • ? - Optional (zero or one)
  • + - One or more
  • [...] - Character class
  • (...) - Capturing group
  • (?:...) - Non-capturing group
  • {n,m} - Quantifier (n to m times)
  • | - Alternation (OR)

Examples

Input Result Explanation
2024-01-15T14:30:00Z ✓ Match UTC datetime
2024-01-15T14:30:00.123Z ✓ Match With milliseconds
2024-01-15T14:30:00+05:30 ✓ Match With timezone offset

Code Examples

JavaScript

JavaScript
const pattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$/;
const text = "2024-01-15T14:30:00Z";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$'
text = "2024-01-15T14:30:00Z"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$/';
$text = "2024-01-15T14:30:00Z";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d{3})?(?:Z|[+-]\\d{2}:\\d{2})$";
String text = "2024-01-15T14:30:00Z";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: APIs, logging, distributed systems

Frequently Asked Questions

What is the regex pattern for iso 8601 datetime regex?
The regex pattern is: ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$. Match full ISO 8601 datetime with timezone. Includes optional milliseconds and timezone offset.
How do I use the iso 8601 datetime regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d{3})?(?:Z|[+-]\\d{2}:\\d{2})$'); or const regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for iso 8601 datetime regex?
APIs, logging, distributed systems

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: