Python
ISO 8601
datetime
time formatting
programming

ISO time ISO 8601 in Python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Python, the easiest way to work with ISO 8601 timestamps is through the datetime module. The two most useful operations are converting a datetime to an ISO 8601 string with isoformat(), and parsing ISO-like strings back into datetime objects with fromisoformat(). The biggest source of confusion is time zones: a timestamp is not fully portable unless you decide whether it should be naive, local, or UTC-aware.

Producing ISO 8601 strings

Python's datetime.isoformat() already generates an ISO-style string.

python
1from datetime import datetime
2
3now = datetime.now()
4print(now.isoformat())

A typical result looks like:

text
2026-03-11T14:30:45.123456

That is a valid ISO 8601 style representation, but it is a naive timestamp because it has no time zone offset attached.

Use timezone-aware datetimes for exchange formats

If the timestamp will be stored, transmitted, or compared across systems, use a timezone-aware datetime. UTC is usually the safest default.

python
1from datetime import datetime, timezone
2
3now_utc = datetime.now(timezone.utc)
4print(now_utc.isoformat())

That gives output like:

text
2026-03-11T19:30:45.123456+00:00

This is much more useful in APIs and logs because it records the offset explicitly.

If you want a trailing Z

Many systems prefer UTC timestamps ending in Z instead of +00:00. Python's isoformat() usually emits the explicit offset form, so a common conversion is:

python
1from datetime import datetime, timezone
2
3timestamp = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
4print(timestamp)

That produces output like:

text
2026-03-11T19:30:45.123456Z

This is still representing UTC; it is just using the shorter UTC marker.

Parsing ISO 8601 strings

For many normal ISO-like strings, fromisoformat() is enough:

python
1from datetime import datetime
2
3dt = datetime.fromisoformat("2026-03-11T14:30:45")
4print(dt)

And for an offset-aware value:

python
1from datetime import datetime
2
3dt = datetime.fromisoformat("2026-03-11T14:30:45+02:00")
4print(dt.tzinfo)

If you need broad compatibility with incoming timestamps that use a trailing Z, normalizing it to +00:00 before parsing is still a practical and explicit approach:

python
1from datetime import datetime
2
3raw = "2026-03-11T19:30:45Z"
4dt = datetime.fromisoformat(raw.replace("Z", "+00:00"))
5print(dt)

That makes the UTC meaning explicit before parsing.

Date-only and time-only values

ISO 8601 is broader than just full timestamps. Python also supports date-only and time-only formatting through related objects.

python
1from datetime import date, time
2
3print(date.today().isoformat())
4print(time(14, 30, 45).isoformat())

This is useful when your application does not need a full timestamp and you want the representation to match the precision of the data.

Choose the right precision

isoformat() also lets you control precision if you do not want microseconds in the output.

python
1from datetime import datetime, timezone
2
3now = datetime.now(timezone.utc)
4print(now.isoformat(timespec="seconds"))

That often makes logs and API payloads easier to read:

text
2026-03-11T19:30:45+00:00

Precision should match the real needs of the system. More digits are not always better.

Common Pitfalls

The biggest mistake is using naive datetimes for data exchange across systems. Without a time zone or offset, the string can be interpreted differently elsewhere.

Another issue is assuming every ISO 8601 variant looks exactly the same. Compact formats, offsets, week dates, and ordinal dates all exist, even if your app only uses the common calendar-timestamp form.

Developers also forget that isoformat() is about representation, not time-zone conversion. If you need UTC output, convert to UTC first.

Finally, do not mix display formatting and machine-readable formatting. ISO 8601 is excellent for interchange, but it is not always the best user-facing date format.

Summary

  • Use datetime.isoformat() to produce ISO 8601 style strings in Python.
  • Prefer timezone-aware datetimes for APIs, logs, and stored timestamps.
  • Replace +00:00 with Z only when you specifically want that UTC style.
  • Use fromisoformat() for common parsing cases, with explicit Z normalization when needed.
  • Keep machine-readable ISO timestamps separate from user-facing display formats.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.