fromisoformat(): parses a date or datetime string in ISO 8601 format

fromisoformat : Return date object by taking date string in YYYY-MM-DD format .

Today's date

We used strftime()
from datetime import date
dt = date.fromisoformat(date.today().strftime('%Y-%m-%d'))
print(dt)
Output
2019-09-17
By using date string
from datetime import date
dt = date.fromisoformat('2019-09-22')
print(dt)
Output is here
2019-09-22
Handling Invalid Date Strings:
from datetime import date
try:
    dt = date.fromisoformat('2019-02-30')  # Invalid date
except ValueError as e:
    print(f"Error: {e}")
Output
Error: day is out of range for month

Difference between fromisoformat() and strptime()

fromisoformat(): A simple, direct method to parse ISO date strings in the YYYY-MM-DD format. It's faster and requires no format specification.

strptime(): More versatile but requires a format string to define the structure of the input. It’s useful for parsing dates in various formats.

from datetime import date, datetime

# Using fromisoformat
print(date.fromisoformat('2023-09-12'))

# Using strptime for the same result
print(datetime.strptime('2023-09-12', '%Y-%m-%d').date())
Output
2023-09-12
2023-09-12

fromisoformat() is simpler when handling standard ISO dates, while strptime() is needed for more complex formats.


All Date Objects
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer