Content · Programming · Python

Python datetime.datetime.fromisoformat() Method: Usage and Examples

Parse supported ISO-style date and time strings into datetime objects.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Parse supported ISO-style date and time strings into datetime objects.

How It Works


datetime.fromisoformat(text) accepts many ISO 8601-compatible date-time representations, including numeric UTC offsets. It is not a universal parser for every possible ISO notation and raises ValueError for invalid input.

Step-by-Step Example


from datetime import datetime
parsed_time = datetime.fromisoformat('2026-10-17T12:30:00+00:00')
print(parsed_time.hour)
print(parsed_time.utcoffset().total_seconds())

Understanding the Example


  • The expression "datetime.fromisoformat('2026-10-17T12:30:00+00:00')" supplies the value stored as 'parsed_time'. This value is then used or checked by the later statements.
  • The print() call exposes the result so you can compare it with the behavior described above. Check the value and its type when experimenting.

A successful run of this example prints the following output:

12
0.0

Where This Fits in a Real Program


In a real import pipeline, parsing is only the first stage. Convert each field to the intended type, reject missing or unexpected values and report which record failed. Keep the original source file unchanged until the normalized data has passed validation and an export has been checked.

Practice: Try It Yourself


Validate the input format and preserve aware offsets during conversion.

For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.

Further Reading


Read the official Python documentation for complete parameters, return types and exceptions.

Common Beginner Mistakes


  • Assuming every free-form timestamp can be parsed by fromisoformat(), or dropping an offset unintentionally.
  • Parse a UTC timestamp and display it in a chosen named time zone.

Key Takeaways


  • Parse supported ISO-style date and time strings into datetime objects.
  • datetime.fromisoformat(text) accepts many ISO 8601-compatible date-time representations, including numeric UTC offsets.
  • Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.
Share this resourceSend this page to someone who may find it useful.