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!
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
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
\d{4} | Year | Exactly 4 digits |
- | Separator | Literal hyphen |
\d{2} | Month | Exactly 2 digits (01-12) |
- | Separator | Literal hyphen |
\d{2} | Day | Exactly 2 digits (01-31) |
T | Date/time separator | Literal T character |
\d{2} | Hour | Exactly 2 digits (00-23) |
: | Separator | Literal colon |
\d{2} | Minutes | Exactly 2 digits (00-59) |
: | Separator | Literal colon |
\d{2} | Seconds | Exactly 2 digits (00-59) |
(?:\.\d+)? | Optional milliseconds | Optional dot and one or more digits |
(?:Z|[+-]\d{2}:\d{2})? | Optional timezone | Z (UTC) or ±HH:MM offset |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
\d{4}-\d{2}-\d{2}- Date part: YYYY-MM-DDT- 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:452024-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
- No date/time validation: Accepts invalid dates like
2024-02-30T12:00:00 - No hour/minute validation: Accepts
2024-03-15T25:00:00(format valid, time invalid) - No timezone validation: Doesn't verify if timezone offset is valid
- Format only: Validates format, not actual datetime validity
- 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,datetimein 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!