Open In App
Related Articles

Isoformat to datetime – Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 module
from datetime import datetime
  
# Getting today's date
todays_Date = datetime.now()
  
# Get date into the isoformat
isoformat_date = todays_Date.isoformat()
  
# print the type of date
print(type(isoformat_date))
  
# convert string date into datetime format
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

# importing datetime module
from datetime import datetime
import dateutil
  
# Getting today's date
todays_Date = datetime.now()
isoformat_date = todays_Date.isoformat()
  
# display isoformat type
print(type(isoformat_date))
  
# convert it into datetime and display
result = dateutil.parser.parse(isoformat_date)
print(type(result))

                    

Output:

<class 'str'>
<class 'datetime.datetime'>


Last Updated : 14 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads