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'>
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Sep, 2021
Like Article
Save Article