Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Convert string to datetime in Python with timezone

  • Last Updated : 14 Sep, 2021

Prerequisite: Datetime module

In this article, we will learn how to get datetime object from time string using Python.

 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

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():

DirectiveMeaningOutput Format
%YAbbreviated weekday name.Sun, Mon, …, Sat
%mMonth as a zero-padded decimal number.01, 02, …, 12
%bMonth as locale’s abbreviated name.Jan, Feb, …, Dec
%dDay of the month as a zero-padded decimal number.01, 02, …, 31
%HHour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12
%MMinute as a zero-padded decimal number.00, 01, …, 59
%SSecond as a zero-padded decimal number.00, 01, …, 59
%fMicrosecond as a decimal number, zero-padded on the left.000000, 000001, …, 999999
%pLocale’s equivalent of either AM or PMAM, PM, am, pm
%zUTC offset in the form ±HHMM+0000, -0400, +0530

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :