Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to convert Python’s .isoformat() string back into datetime object

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will learn how to convert Pythons .isoFormat() string back into a datetime object. Here we are using the current time and for that, we will store the current time in the current_time variable.

The function now() of this module, does the job perfectly.

Example: Getting current time

Python3




from datetime import datetime
  
current_time = datetime.now()
  
print(current_time)

Output:

2021-07-22 15:17:19.735037

Get the string format of time

In this step, we are going to get the string format of the iso time. For this, isoformat() function is used.

Example: getting string format of time

Python3




from datetime import datetime
  
current_time = datetime.now().isoformat()
print(current_time)

Output

2021-07-26T11:36:17.090181

Get the DateTime object from the iso format

So here we are going to get the DateTime object from the iso string. For that fromisoformat() function is used.

Example: Get the DateTime object from the iso format

Python3




from datetime import datetime
  
current_time = datetime.now().isoformat()
  
print(datetime.fromisoformat(current_time))

Output:

2021-07-26 17:06:51.149731

My Personal Notes arrow_drop_up
Last Updated : 28 Jul, 2021
Like Article
Save Article
Similar Reads
Related Tutorials