The fromisoformat()
method from the date
class is used to generate a date
object to convert the argument string (ISO format). It takes a string argument and converts it into an ISO calendar date
instance. The standard
Note: The
date
class is obtained fromdatetime
module.
fromisoformat(date_string)
This method takes a single argument value:
date_string
: It shows the defined date
type object will be created.date-type-object
: It will transform a string argument value (
The codes below show how fromisformat()
behaves.
# Import the datetime moduleimport datetime# intialize the dateISO_date = "2021-12-18";# Call the fromisoformat() method to create the datetime.date objectgregorian_date = datetime.date.fromisoformat(ISO_date);# Print the newly created date objectprint("String as Date object: %s" %gregorian_date);
ISO_date
string type variable that contains 2021-12-18
in ISO format.date
type object in gregorian_date
variable.# Import date class from datetime modulefrom datetime import date# converting string argument in date format# to date type objectdate_object = date.fromisoformat('2021-12-25')# Print the newly created date objectprint("The newly created date object is : %s" %date_object);
'2021-12-25'
to date_object.date_object
on console.# Import date class from datetime modulefrom datetime import date# intialize today variable with today's date# converting date to a String objecttoday= str(date.today());# printing date as stringprint("Date as string: %s" %today)# invoking fromisoformat() method# to create the datetime.date type objectmydate = date.fromisoformat(today);# Print the newly created date objectprint("The newly created date object is : %s" %mydate);
date.today()
method is extracting today’s date and returning a date object
. Then, str()
is converting date object
to a string object today
.today
on the console.fromisoformat()
method from date class and converting today
(from ISO formatted date) to mydate
as a date object.mydate
on the console.