Back to Home

ISO 8601 DateTime Validation Regex

CronOS Team
regexdatetimevalidationtutorialiso-8601timezone

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern

ISO 8601 DateTime Validation Regex

Validate dates and times in ISO 8601 format with optional timezone, milliseconds, and time components using regex.

Pattern Breakdown

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

Components

ComponentDescriptionMatches
^Start anchorEnsures match from string start
\d{4}YearExactly 4 digits
-SeparatorLiteral hyphen
\d{2}MonthExactly 2 digits (01-12)
-SeparatorLiteral hyphen
\d{2}DayExactly 2 digits (01-31)
TDate/time separatorLiteral T character
\d{2}HourExactly 2 digits (00-23)
:SeparatorLiteral colon
\d{2}MinutesExactly 2 digits (00-59)
:SeparatorLiteral colon
\d{2}SecondsExactly 2 digits (00-59)
(?:\.\d+)?Optional millisecondsOptional dot and one or more digits
(?:Z|[+-]\d{2}:\d{2})?Optional timezoneZ (UTC) or ±HH:MM offset
$End anchorEnsures match to string end

Detailed Breakdown

  • \d{4}-\d{2}-\d{2} - Date part: YYYY-MM-DD
  • T - ISO 8601 date/time separator (literal T)
  • \d{2}:\d{2}:\d{2} - Time part: HH:MM:SS
  • (?:\.\d+)? - Optional milliseconds:
    • (?:...) - Non-capturing group
    • \. - Literal dot
    • \d+ - One or more digits
    • ? - Makes entire group optional
  • (?:Z|[+-]\d{2}:\d{2})? - Optional timezone:
    • Z - UTC timezone indicator
    • \| - OR operator
    • [+-] - Plus or minus sign
    • \d{2}:\d{2} - Timezone offset HH:MM
    • ? - Makes entire group optional

Examples

Valid:

  • 2024-03-15T12:30:45
  • 2024-03-15T12:30:45Z (UTC)
  • 2024-03-15T12:30:45+05:30 (IST)
  • 2024-03-15T12:30:45-08:00 (PST)
  • 2024-03-15T12:30:45.123Z (with milliseconds)
  • 2024-03-15T12:30:45.123456789+05:30 (with nanoseconds)
  • 2024-03-15T00:00:00Z (midnight UTC)

Invalid:

  • 2024-3-15T12:30:45 (month not zero-padded)
  • 2024-03-15 12:30:45 (space instead of T)
  • 2024-03-15T12:30:45+5:30 (timezone offset not zero-padded)
  • 2024-03-15T25:00:00 (invalid hour)
  • 2024-13-15T12:30:45 (invalid month)
  • 24-03-15T12:30:45 (year too short)

Implementation

JavaScript

javascript
const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?$/;
iso8601Regex.test('2024-03-15T12:30:45Z'); // true
iso8601Regex.test('2024-03-15T12:30:45+05:30'); // true
iso8601Regex.test('2024-03-15T12:30:45.123Z'); // true
iso8601Regex.test('2024-03-15T25:00:00'); // false (invalid hour)

Python

python
import re
iso8601_regex = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?$'
bool(re.match(iso8601_regex, '2024-03-15T12:30:45Z'))  # True
bool(re.match(iso8601_regex, '2024-03-15T12:30:45+05:30'))  # True
bool(re.match(iso8601_regex, '2024-03-15T25:00:00'))  # False (invalid hour)

Go

go
iso8601Regex := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?$`)
iso8601Regex.MatchString("2024-03-15T12:30:45Z") // true
iso8601Regex.MatchString("2024-03-15T12:30:45+05:30") // true
iso8601Regex.MatchString("2024-03-15T25:00:00") // false (invalid hour)

Limitations

  1. No date/time validation: Accepts invalid dates like 2024-02-30T12:00:00
  2. No hour/minute validation: Accepts 2024-03-15T25:00:00 (format valid, time invalid)
  3. No timezone validation: Doesn't verify if timezone offset is valid
  4. Format only: Validates format, not actual datetime validity
  5. No week dates: Doesn't support ISO 8601 week dates (YYYY-Www-D)

When to Use

  • ISO 8601 datetime format validation
  • API datetime parameters
  • Database datetime field validation
  • International datetime standards
  • When you need timezone-aware datetimes
  • Logging and timestamp formats

For production, consider:

  • Additional validation for actual date/time validity
  • Using datetime parsing libraries (e.g., Date.parse() in JS, datetime in Python)
  • Validating timezone offsets
  • Handling edge cases (leap seconds, etc.)
  • Combining with datetime object validation

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern