Simple module to parse ISO 8601 dates

2.1.0 · active · verified Sun Apr 05

This module parses the most common forms of ISO 8601 date strings (e.g., 2007-01-14T20:34:22+00:00) into `datetime` objects. It simplifies the process of converting various ISO 8601 date and time formats into Python's native `datetime` types. The library is actively maintained, with a typical release cadence of a few minor/patch versions per year, ensuring compatibility and bug fixes.

Warnings

Install

Imports

Quickstart

The `parse_date` function is the main entry point for parsing ISO 8601 strings. It automatically handles various common formats including full datetimes with and without timezones, and date-only strings. By default, missing timezone information will result in a UTC-aware datetime object unless a `default_timezone` is specified or set to `None` for naive datetimes.

import iso8601
from datetime import datetime, timezone

# Parse a full ISO 8601 datetime string with UTC timezone
datetime_str_utc = '2023-10-27T10:00:00Z'
datetime_obj_utc = iso8601.parse_date(datetime_str_utc)
print(f"Parsed UTC: {datetime_obj_utc}")
assert datetime_obj_utc == datetime(2023, 10, 27, 10, 0, tzinfo=timezone.utc)

# Parse a datetime string with an offset timezone
datetime_str_offset = '2023-10-27T10:00:00-05:00'
datetime_obj_offset = iso8601.parse_date(datetime_str_offset)
print(f"Parsed Offset: {datetime_obj_offset}")

# Parse a date-only string (time defaults to 00:00:00 UTC)
date_str = '2023-10-27'
datetime_obj_date_only = iso8601.parse_date(date_str)
print(f"Parsed Date Only: {datetime_obj_date_only}")
assert datetime_obj_date_only == datetime(2023, 10, 27, 0, 0, tzinfo=iso8601.UTC)

# Parse a date with a space separator instead of 'T'
datetime_str_space = '2023-10-27 10:00:00+01:00'
datetime_obj_space = iso8601.parse_date(datetime_str_space)
print(f"Parsed with Space: {datetime_obj_space}")

view raw JSON →