Convert string to datetime in Python with timezone - GeeksforGeeks

14 Sep, 2021

Prerequisite: Datetime module
In this article, we will learn how to get datetime object from time string using Python.
To convert a time string into datetime object, datetime.strptime() function of datetime module is used. This function returns datetime object. 
Syntax : datetime.strptime(date_string, format)
Parameters :
date_string : Specified time string containing of which datetime object is required. **Required
format : Abbreviation format of date_string
Returns : Returns the datetime object of given date_string
List of the acceptable format that can be passed to strptime():
Example 1: Converting datetime string to datetime
Python3
 

 
 










# Python3 code to demonstrate 
# Getting datetime object using a date_string 
  
# importing datetime module 
import datetime 
  
# datestring for which datetime_obj required 
date_string = '2021-09-01 15:27:05.004573 +0530'
print("string datetime: ") 
print(date_string) 
print("datestring class is :", type(date_string)) 
  
# using strptime() to get datetime object 
datetime_obj = datetime.datetime.strptime( 
    date_string, '%Y-%m-%d %H:%M:%S.%f %z') 
  
print("converted to datetime:") 
  
# Printing datetime 
print(datetime_obj) 
  
# Checking class of datetime_obj. 
print("datetime_obj class is :", type(datetime_obj)) 






Output:
string datetime: 
2021-09-01 15:27:05.004573 +0530
datestring class is : <class 'str'>
converted to datetime:
2021-09-01 15:27:05.004573+05:30
datetime_obj class is : <class 'datetime.datetime'>
Example 2: Converting datetime string to datetime
Python3
 

 
 










# Python3 code to demonstrate 
# Getting datetime object using a date_string 
  
# importing datetime module 
import datetime 
  
# datestring for which datetime_obj required 
date_string = '2021-09-01 15:27:05'
  
# using strptime() to get datetime object 
datetime_obj = datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S') 
  
# Printing datetime 
print(datetime_obj) 






Output:
2021-09-01 15:27:05
In this example, microseconds and time zone parts are removed from first example, so we need to remove microseconds and time zone abbreviations also from format string
Example 3: Converting datetime string to datetime
Python3
 

 
 










# Python3 code to demonstrate 
# Getting datetime object using a date_string 
  
# importing datetime module 
import datetime 
  
# datestring for which datetime_obj required 
date_string = 'Sep 01 2021 03:27:05 PM'
  
# using strptime() to get datetime object 
datetime_obj = datetime.datetime.strptime(date_string, '%b %d %Y %I:%M:%S %p') 
  
# Printing datetime 
print(datetime_obj) 






Output:
2021-09-01 15:27:05

Picked


Python datetime-program


Python

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python