Explain Codes LogoExplain Codes Logo

How do I parse an ISO 8601-formatted date?

python
date-parsing
datetime
error-handling
Nikita BarsukovbyNikita Barsukov·Aug 14, 2024
TLDR

Use datetime.datetime.fromisoformat() for Python 3.7+ to transform an ISO 8601 date string into a datetime object:

from datetime import datetime parsed_date = datetime.fromisoformat("2023-03-20T10:30:00")

For older versions, you need to install python-dateutil and use the dateutil.parser.parse() method:

from dateutil.parser import parse parsed_date = parse("2023-03-20T10:30:00")

In Python 3.11, the datetime.fromisoformat() method received an upgrade, now allowing for better timezone parsing, among other new features. Remember to also catch up on the changes if you've been using Python ≤ 3.10!

Digging into date parsing in Python

There's more to parsing dates than just built-in modules. Let's explore this topic more deeply.

Python's built-in approaches

Python 3.7 introduced datetime.fromisoformat(), designed for speedy and easy parsing of ISO 8601 date strings. But heed the limitations in versions ≤ 3.10: they don't support ordinal dates or fractional hours/minutes.

With Python 3.11, you can now handle a broader range of valid ISO 8601 strings, such as time zones with fractional seconds.

When dateutil comes to the rescue

The python-dateutil library offers two beneficial functions: parse() and isoparse(). Both are less strict than the built-in methods, hence they offer flexibility in parsing slightly off-standard strings.

The isoparse() method is your friend when dealing with RFC 3339 formatted strings, while parse() gives more leniency for other strings.

Precision with strptime

If you're the kind of person who likes more control over patterns, Python's datetime.strptime() allows for custom format strings. It proves useful for RFC 3339 formatted strings with a format string like "%Y-%m-%dT%H:%M:%S.%fZ".

However, to handle timezone offsets like "+0500" without a colon, you might need to get your hands dirty with additional logic.

Mastering advanced parsing scenarios

Editing datetime strings can be a complex task. Brace yourself for edge cases and advanced scenarios.

Timezone shenanigans

If you are in global business, timezone awareness is crucial. Python 3.7's fromisoformat() has been improved to handle timezone information better, albeit with a permissive nature.

When dealing with timezone offsets in RFC 3339 strings, you might need to manually parse these values or use more robust libraries such as python-dateutil.

Error-proofing your code

As a professional developer, you know that you should always implement a try-except block when dealing with unpredictable date formats. This helps prevent your application from crashing due to ValueErrors which tend to appear with non-compliant strings.

Decoding non-standard formats

There might be times when you find odd separators or unexpected formats in your dates. Whether you're crafting custom parsing logic using strptime or bending libraries to your will, maintaining RFC 3339 compliance is essential.