Skip to content
Related Articles

Related Articles

Improve Article

Isoformat to datetime – Python

  • Last Updated : 14 Sep, 2021
Geek Week

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'>

 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :