How to Turn a Date into ISO 8601 Text in Python with isoformat()
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
<date or datetime>.isoformat()
| Part | What 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
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())

tzinfo=UTC tells Python that the time is in UTC.
Step 2: Run It
uv run python isoformat.py
2026-11-30
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
| Mistake | What happens | Do 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 in | Give the datetime a time zone, such as tzinfo=UTC |
| Building the text yourself with an f-string | Easy to forget the leading zeros: 2026-9-5 instead of 2026-09-05 | Use isoformat(), which always pads |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The text has no +00:00 | The datetime has no time zone | Add one with tzinfo= or create it with datetime.now(UTC) |
You need a different layout, such as 30 Nov 2026 | ISO 8601 has one fixed layout | Use 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
- date.isoformat() — the date version.
- datetime.isoformat() — the datetime version and its options.
