datetime.datetime.fromisoformat
Convert a string in ISO 8601 format to a datetime object.
Python 3.13
datetime.datetime.fromisoformat(date_string)Parameters
- date_stringstr
- A string such as 2026-05-20, 2026-05-20T14:30:00 or one with a +03:00 offset.
Return value
A datetime object; raises ValueError for a string that is not in ISO format.
Examples
Date and time from an ISO string
Python 3.13
from datetime import datetime
print(datetime.fromisoformat('2026-05-20T14:30:00'))2026-05-20 14:30:00
String with a time zone offset
Python 3.13
from datetime import datetime
moment = datetime.fromisoformat('2026-05-20T14:30:00+03:00')
print(moment.tzinfo)
print(moment.isoformat())UTC+03:00 2026-05-20T14:30:00+03:00
Details
It understands the strings produced by the isoformat() method and, since Python 3.11, almost any ISO 8601 format, including a trailing Z. If the string has an offset, the result has a time zone. For other formats, such as 20.05.2026, use strptime.
