Fromisoformat() Function of Datetime.date Class in Python

The datetime.date class in Python provides a convenient way to represent and manipulate dates. The fromisoformat() method allows you to create a date object from a string in ISO 8601 format ("YYYY-MM-DD").

This method is particularly useful when parsing dates from log files, APIs, or any data source that provides dates in the standard ISO format.

Syntax

datetime.date.fromisoformat(date_string)

Parameters:

  • date_string ? A string representing a date in ISO format "YYYY-MM-DD"

Return Value: Returns a datetime.date object representing the parsed date.

Example 1: Creating a Date Object from ISO String

Let's create a date object from an ISO formatted string ?

from datetime import date

# ISO formatted date string
date_string = "2023-03-31"

# Convert string to date object
date_obj = date.fromisoformat(date_string)

print("Original string:", date_string)
print("Date object:", date_obj)
print("Type:", type(date_obj))
Original string: 2023-03-31
Date object: 2023-03-31
Type: <class 'datetime.date'>

Example 2: Converting Multiple Date Strings

Here's how to convert a list of ISO date strings to date objects ?

from datetime import date

date_strings = ['2022-01-01', '2022-06-15', '2022-12-31']
date_objects = []

for date_str in date_strings:
    date_objects.append(date.fromisoformat(date_str))

print("Original strings:", date_strings)
print("Date objects:", date_objects)

# Using list comprehension (more Pythonic)
date_objects_2 = [date.fromisoformat(d) for d in date_strings]
print("Using list comprehension:", date_objects_2)
Original strings: ['2022-01-01', '2022-06-15', '2022-12-31']
Date objects: [datetime.date(2022, 1, 1), datetime.date(2022, 6, 15), datetime.date(2022, 12, 31)]
Using list comprehension: [datetime.date(2022, 1, 1), datetime.date(2022, 6, 15), datetime.date(2022, 12, 31)]

Example 3: Working with Today's Date

Converting today's date from string format back to a date object ?

from datetime import date

# Get today's date and convert to string
today_str = str(date.today())
print("Today as string:", today_str)

# Convert back to date object
today_obj = date.fromisoformat(today_str)
print("Today as date object:", today_obj)

# Demonstrate it's the same as direct today()
direct_today = date.today()
print("Direct today():", direct_today)
print("Are they equal?", today_obj == direct_today)
Today as string: 2023-05-20
Today as date object: 2023-05-20
Direct today(): 2023-05-20
Are they equal? True

Error Handling

The method raises ValueError if the string doesn't follow the ISO format ?

from datetime import date

# Valid formats
valid_dates = ["2023-01-01", "2023-12-31"]

# Invalid formats
invalid_dates = ["01-01-2023", "2023/01/01", "January 1, 2023", "2023-13-01"]

print("Valid dates:")
for date_str in valid_dates:
    try:
        result = date.fromisoformat(date_str)
        print(f"  {date_str} ? {result}")
    except ValueError as e:
        print(f"  {date_str} ? Error: {e}")

print("\nInvalid dates:")
for date_str in invalid_dates:
    try:
        result = date.fromisoformat(date_str)
        print(f"  {date_str} ? {result}")
    except ValueError as e:
        print(f"  {date_str} ? Error: Invalid format")
Valid dates:
  2023-01-01 ? 2023-01-01
  2023-12-31 ? 2023-12-31

Invalid dates:
  01-01-2023 ? Error: Invalid format
  2023/01/01 ? Error: Invalid format
  January 1, 2023 ? Error: Invalid format
  2023-13-01 ? Error: Invalid format

Key Points

  • Only accepts strings in "YYYY-MM-DD" format
  • Raises ValueError for invalid formats or dates
  • Returns a proper datetime.date object
  • Validates date components (month 1-12, day within month limits)
  • More reliable than manual string parsing

Conclusion

The fromisoformat() method provides a reliable way to convert ISO-formatted date strings into datetime.date objects. It automatically validates the format and date components, making it safer than manual parsing for handling standardized date inputs.

---
Updated on: 2026-03-27T13:21:16+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements