Dan Bader

Parsing ISO 8601 timestamps in plain Django

“How do I parse an ISO 8601 formatted date in Django without bringing in extra dependencies?”

If you do any web development with Python and Django then you’ll inevitable find yourself wanting to parse ISO 8601 timestamps into Python’s native datetime.datetime objects at some point. In other words, given a timestamp string like '2016-12-11T09:27:24.895' we want to convert it into a proper Python datetime object for further processing.

If you search Google on how to do this you’ll often find people recommend the 3rd party python-dateutil module. Python-dateutil is a great choice – but in some cases it does more than you really need.

If you’re already using Django you can parse ISO 8601 timestamps without bringing in another dependency using django.utils.dateparse.parse_datetime.

Here’s how:

from django.utils.dateparse import parse_datetime
parsed = parse_datetime('2001-12-11T09:27:24.895551')
assert parsed == datetime(2001, 12, 11, 9, 27, 20, 608645)

Note that if you pass a malformed value to parse_datetime it can throw a KeyError, ValueError, or TypeError exception so you might want to be ready to handle those.

Importantly, parse_datetime also understands timezone-aware timestamps and correctly sets the UTC offset on the resulting datetime object:

from django.utils.dateparse import parse_datetime
from django.utils.timezone import is_aware, utc

expected = datetime.datetime(2016, 4, 27, 11, 18, 42, 303886, tzinfo=utc)
parsed = parse_datetime('2016-04-27T11:18:42.303886+00:00')

assert parsed == expected
assert is_aware(parsed)

Improve Your Python with a fresh 🐍 Python Trick 💌 every couple of days

🔒 No spam ever. Unsubscribe any time.

This article was filed under: django, python, and web-development.

Related Articles:
Latest Articles:
  • Context Managers and the “with” Statement in Python – The “with” statement in Python is regarded as an obscure feature by some. But when you peek behind the scenes of the underlying Context Manager protocol you’ll see there’s little “magic” involved.
  • Installing Python and Pip on Windows – In this tutorial you’ll learn how to set up Python and the Pip package manager on Windows 10, completely from scratch.
  • Sublime Text Settings for Writing Clean Python – How to write beautiful and clean Python by tweaking your Sublime Text settings so that they make it easier to adhere to the PEP 8 style guide recommendations.
  • How Sublime Text’s Preferences Work – Sublime Text uses a powerful text-based settings system that can be a little intimidating when you encounter it for the first time. This article gives an overview of the system and points out some common mistakes to avoid.
  • Writing Clean Python With Namedtuples – Python comes with a specialized “namedtuple” container type that doesn’t seem to get the attention it deserves. It’s one of these amazing features in Python that’s hidden in plain sight.
  • Lambda Functions in Python: What Are They Good For? – An introduction to “lambda” expressions in Python: What they’re good for, when you should use them, and when it’s best to avoid them.
← Browse All Articles