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 lets in you to create a date object from a string in the ISO layout.

The fromisoformat() feature will parse a string in ISO 8601 format and create a date object that represents that date. this could be beneficial in numerous scenarios, consisting of whilst you want to parse dates from log files or whilst working with APIs that offer dates in ISO 8601 format.

Syntax

datetime.date.fromisoformat(date_string)

The parameter, date_string is a string that represents a date in the ISO format "YYYY-MM-DD".

Example 1

We will be creating a date object from a string with the help of the fromisoformat() method.

Algorithm

  • Load the datetime module

  • Initialise a string variable containing a date in ISO layout

  • Convert the string to a date object using the fromisoformat() function

  • Print the date object

from datetime import date
# date passed as string data type
date_string = "2023-03-31"

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

print(date_obj)

Output

2023-03-31

When the fromisoformat() is called, the ISO formatted string is passed as an argument to the function which strictly follows the format ‘YYYY-MM-DD’. The string is parsed and the date components are extracted by year, month and date. Similarly, time can also be extracted by hours, minutes and seconds. An equivalent datetime object is returned if the parsed string follows the ISO 8601 format strictly.

If the format followed did not abide by the ISO format , then ValueError is raised. This indicates that the month date and year did not follow the sequence correctly or went out of the expected range.

Example 2

The following example converts multiple string objects (date) stored in lists to date objects.

Algorithm

  • Import the datetime module

  • Define a list variable containing multiple strings in ISO format

  • Make an empty list to keep the date objects

  • Loop via every string within the list and use the fromisoformat() function to convert it to a date object

  • Append the date object to the list

  • Print the list to the user

import datetime

date_strs = ['2022-01-01', '2022-01-02', '2022-01-03']
date_objs = []

for date_str in date_strs:
   date_objs.append(datetime.date.fromisoformat(date_str))

print(date_objs)

Output

[datetime.date(2022, 1, 1), datetime.date(2022, 1, 2), datetime.date(2022, 1, 3)]

Example 3

The following example converts today’s date and present time stored in a string to date object.

Algorithm

  • Import the datetime and time module

  • Get today’s date as a string using date.today()

  • Print today’s date in ISO format string

  • Use the date.fromisoformat() function to convert today into a date object.

  • Print the date object to the user.

from datetime import date
today= str(date.today());
print("Today's Date as string: %s" %today)
mydate = date.fromisoformat(today);
print(mydate);

Output

Today's Date as string: 2023-05-20
2023-05-20

Conclusion

The conversion of a string containing an ISO-formatted date into a date item that may be used for similar operations is made easy and powerful by using this function.

The manual parsing of ISO formatted strings, that is at risk of errors, may be prevented by using the fromisoformat() feature. additionally, this function makes positive that the generated date object is valid and complies with ISO requirements.

Moreover, Python's datetime module has the fromisoformat() feature and offers a huge range of utilities for operating with dates and times. This module permits us to format dates, translate between one-of-a-kind time zones, and perform a selection of date and time computations.

Updated on: 23-Aug-2023

469 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements