Skip to main content

How to Turn a Date into ISO 8601 Text in Python with isoformat()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

APIs, databases and log files agree on one way to write dates: ISO 8601, as in 2026-11-30 or 2026-11-30T03:07:41+00:00. Python writes that text for you with isoformat(), on both date and datetime objects. This guide shows both on Python 3.15.2.

Syntax​

Syntax
<date or datetime>.isoformat()
PartWhat it means
<date or datetime>A date, such as date(2026, 11, 30), or a datetime
.isoformat()Returns the value as ISO 8601 text: YYYY-MM-DD for a date, the date plus T and the time for a datetime

For a date, Python's datetime documentation says it returns "a string representing the date in ISO 8601 format, YYYY-MM-DD".

The Example​

The script writes a date as ISO text, then a datetime in UTC, which adds the time and the +00:00 offset.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

isoformat.py
from datetime import UTC, date, datetime

launch = date(2026, 11, 30)
print(launch.isoformat())

moment = datetime(2026, 11, 30, 3, 7, 41, tzinfo=UTC)
print(moment.isoformat())

The isoformat.py script in a macOS-style code window: it prints date(2026, 11, 30).isoformat() and the isoformat() of a datetime at 03:07:41 on the same day in UTC.

tzinfo=UTC tells Python that the time is in UTC.

Step 2: Run It​

Terminal
uv run python isoformat.py
Output
2026-11-30
2026-11-30T03:07:41+00:00

The terminal after uv run python isoformat.py: 2026-11-30, then 2026-11-30T03:07:41+00:00.

The date became 2026-11-30. The datetime became the date, a T, the time, and +00:00, its offset from UTC. Text in this form can go straight into a URL, a JSON field or a database column. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
A datetime without a time zone: datetime(2026, 11, 30, 3, 7, 41).isoformat()The text is 2026-11-30T03:07:41, with no offset, so whoever reads it can't tell which time zone it's inGive the datetime a time zone, such as tzinfo=UTC
Building the text yourself with an f-stringEasy to forget the leading zeros: 2026-9-5 instead of 2026-09-05Use isoformat(), which always pads

Troubleshooting​

SymptomLikely causeFix
The text has no +00:00The datetime has no time zoneAdd one with tzinfo= or create it with datetime.now(UTC)
You need a different layout, such as 30 Nov 2026ISO 8601 has one fixed layoutUse strftime with your own format instead

FAQ​

How do I turn ISO text back into a date?​

date.fromisoformat("2026-11-30") does the reverse, and datetime.fromisoformat() does the same for a datetime.

Is isoformat() the same as str()?​

Not quite. For a datetime, str() puts a space between the date and the time instead of T, as in 2026-11-30 03:07:41+00:00. isoformat() uses T.

Conclusion​

date(2026, 11, 30).isoformat() gave 2026-11-30, and a UTC datetime gave 2026-11-30T03:07:41+00:00. Give datetimes a time zone so the text says which one it means.

Additional Resources​