Python datetime.fromisoformat() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
classmethod

Definition

datetime.fromisoformat(date_string) parses an ISO 8601 string and returns a datetime. If the text ends with an offset such as +05:30, the result is aware; otherwise it is naive.

On Python 3.7 to 3.10 it only accepts the formats isoformat() can produce. Python 3.11 accepts most ISO 8601 text, including a trailing Z and compact forms like 20240315T103000. date.fromisoformat() and time.fromisoformat() parse just a date or a time.

datetime.fromisoformat() is a classmethod of datetime.datetime: call it on the class itself, as datetime.fromisoformat(...), and it returns a new object. Import the class first, for example from datetime import datetime. The date and time classes have a method with the same name. Added in Python 3.7.

Syntax

Pass the ISO 8601 text.

python
moment = datetime.fromisoformat(date_string)
day = date.fromisoformat("2024-03-15")
CallWhat it does
datetime.fromisoformat("2024-03-15T10:30:45")Returns datetime.datetime(2024, 3, 15, 10, 30, 45).
date.fromisoformat("2024-03-15")Returns datetime.date(2024, 3, 15).
datetime.fromisoformat("2024-03-15 10:30+05:30")Returns datetime.datetime(2024, 3, 15, 10, 30, tzinfo=datetime.timezone(datetime.timedelta(seconds=19800))).

Any single character may separate the date and the time, but T and a space are the usual choices.

Method signatures
python
classmethod datetime.fromisoformat(date_string)
classmethod date.fromisoformat(date_string)
classmethod time.fromisoformat(time_string)
# returns: datetime / date / time

Parameters

ParameterDescription
date_stringA str in ISO 8601 form, such as "2024-03-15", "2024-03-15T10:30:45.250", or "2024-03-15T10:30+05:30".

Return Value

Return ValueDescription
datetimeAware when the text includes an offset; naive otherwise. A missing time means midnight.

Python Examples

Example 1 — Parse Dates and Date-Times from ISO Text

Parse a date, a full date-time, a date-time with a space separator, and a date-only string with the datetime class.

python
from datetime import date, datetime

print(date.fromisoformat("2024-03-15"))
print(datetime.fromisoformat("2024-03-15T10:30:45"))
print(datetime.fromisoformat("2024-03-15 10:30"))
print(repr(datetime.fromisoformat("2024-03-15")))

How It Works

  1. Date only. date.fromisoformat() reads YYYY-MM-DD into a date.
  2. Full timestamp. T separates the date from the time, as in standard ISO 8601.
  3. Seconds optional. 10:30 without seconds is valid and fills in zero seconds; the space separator also works.
  4. Midnight default. A date-only string given to datetime.fromisoformat() becomes 00:00 on that day.

Example 2 — Parse an Offset and Milliseconds

Parse text with three fractional digits and a +05:30 offset, then convert the aware result to UTC.

python
from datetime import datetime, timezone

stamp = datetime.fromisoformat("2024-03-15T10:30:45.250+05:30")

print(stamp.microsecond)
print(stamp.utcoffset())
print(stamp.astimezone(timezone.utc).isoformat())

How It Works

  1. Milliseconds scale up. .250 means 250 milliseconds, stored as microsecond=250000.
  2. Offset becomes tzinfo. The +05:30 suffix creates a fixed-offset timezone, so utcoffset() returns 5:30:00.
  3. Convert to UTC. Subtracting 5 hours 30 minutes gives 05:00:45.250 UTC, written back with six fractional digits.

Example 3 — Accept a Trailing Z on Older Pythons

Python 3.11+ parses Z directly. This helper rewrites it to +00:00 so the same code runs on 3.7 to 3.10 too, and reports text that is not ISO 8601.

python
from datetime import datetime


def parse_iso(text):
    if text.endswith("Z"):
        text = text[:-1] + "+00:00"
    return datetime.fromisoformat(text)


for raw in ("2024-03-15T10:30:00Z", "2024-03-15T10:30:00+01:00", "15/03/2024"):
    try:
        print(parse_iso(raw).isoformat())
    except ValueError:
        print("not ISO 8601:", raw)

How It Works

  1. Z means UTC. The helper swaps the trailing Z for +00:00, which every supported version understands.
  2. Offsets kept. +01:00 parses into an aware datetime and isoformat() writes the same offset back.
  3. Wrong layout. Day/month/year text raises ValueError, which the loop reports instead of crashing.

Frequently Asked Questions

Use datetime.fromisoformat("2024-03-15T10:30:00"). For a date only, use date.fromisoformat("2024-03-15").
Before Python 3.11 the Z suffix was not accepted. Replace it with +00:00 first, or upgrade to 3.11+.
Usually yes, because it parses one fixed grammar instead of interpreting a format string.
Only if the text contains an offset such as +05:30. Otherwise the result is naive.
That is not ISO 8601. Use datetime.strptime(text, "%d/%m/%Y").

Next: date.fromordinal()

Classmethod: build a date from a proleptic Gregorian day number (1 = 0001-01-01).

fromordinal() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful