Isoformat to datetime – Python
Last Updated :
14 Sep, 2021
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
from datetime import datetime
todays_Date = datetime.now()
isoformat_date = todays_Date.isoformat()
print ( type (isoformat_date))
result = 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
from datetime import datetime
import dateutil
todays_Date = datetime.now()
isoformat_date = todays_Date.isoformat()
print ( type (isoformat_date))
result = dateutil.parser.parse(isoformat_date)
print ( type (result))
|
Output:
<class 'str'>
<class 'datetime.datetime'>
Please Login to comment...