Backport of datetime.fromisoformat

2.0.3 · active · verified Thu Apr 09

This library provides a backport of Python 3.11's extended `datetime.fromisoformat` functionality to older Python versions (prior to 3.11). It allows parsing a wider range of ISO 8601 formatted strings, including those with timezone offsets using colons and fractional seconds. The current version is 2.0.3, with releases occurring on an as-needed basis to address compatibility or new Python version changes.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the backported `fromisoformat` method. By importing `backports.datetime_fromisoformat` after `datetime`, it monkey-patches the standard library's `datetime` objects to accept ISO 8601 strings that conform to Python 3.11's broader parsing capabilities, such as timezone offsets with colons.

import datetime
import backports.datetime_fromisoformat

# This string would fail on Python < 3.11 without the backport due to the colon in the timezone offset
isodate_str = "2023-10-27T10:00:00.123456+05:30"

try:
    dt_object = datetime.datetime.fromisoformat(isodate_str)
    print(f"Successfully parsed: {dt_object}")
    print(f"Type: {type(dt_object)}")
except ValueError as e:
    print(f"Error parsing date: {e}")

view raw JSON →