Isoformat to datetime – Python
In this article, we are going to see how to convert Isoformat to datetime in Python.
Method 1: Using fromisoformat() in datetime module
In this method, we are going to use fromisoformat() which converts the string into DateTime object.
Syntax:
datetime.fromisoformatc(date)
Example: Converting isoformat to datetime
Python3
# importing datetime modulefrom datetime import datetime # Getting today's datetodays_Date = datetime.now() # Get date into the isoformatisoformat_date = todays_Date.isoformat() # print the type of dateprint(type(isoformat_date)) # convert string date into datetime formatresult = datetime.fromisoformat(isoformat_date)print(type(result)) |
Output:
<class 'str'> <class 'datetime.datetime'>
Method 2: Using parse() in dateutil module
In this method, we will use this inbuilt Python library python-dateutil, The parse() method can be used to detect date and time in a string.
Syntax: dateutil.parser.parse((isoformat_string)
Parameters: Isoformat_string: Str.
Return: Datetime object
Example: converting isoformat to datetime
Python3
# importing datetime modulefrom datetime import datetimeimport dateutil # Getting today's datetodays_Date = datetime.now()isoformat_date = todays_Date.isoformat() # display isoformat typeprint(type(isoformat_date)) # convert it into datetime and displayresult = dateutil.parser.parse(isoformat_date)print(type(result)) |
Output:
<class 'str'> <class 'datetime.datetime'>
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course



