Convert string to datetime in Python with timezone
Last Updated :
23 Jul, 2025
Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone. Let’s explore some common methods to perform this conversion.
Using datetime.strptime()
datetime.strptime() is fast and ideal for structured data like logs or API responses but requires the exact format of the input string. Timezone parsing is supported using the %z directive.
Python
import datetime
s = '2021-09-01 15:27:05.004573 +0530'
res = datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S.%f %z')
print(res)
Output2021-09-01 15:27:05.004573+05:30
Explanation: string s is parsed into a datetime object using datetime.strptime(), which requires an exact format string ('%Y-%m-%d %H:%M:%S.%f %z') to correctly interpret each component of the date, time and timezone.
Using dateutil.parser.parse()
dateutil.parser.parse() from the python-dateutil library automatically detects and parses date formats without requiring a specified format. It is highly flexible, handling varied or inconsistent date inputs and supports seamless timezone parsing.
Python
from dateutil import parser
s = '2021-09-01 15:27:05.004573 +0530'
res = parser.parse(s)
print(res)
Output2021-09-01 15:27:05.004573+05:30
Explanation: parser.parse(s) automatically detects and parses the components of the date-time string s into a datetime object without needing a format string, handling the timezone seamlessly.
Using pandas.to_datetime()
pandas.to_datetime() is a powerful method from the pandas library, optimized for parsing large volumes of date strings, particularly within DataFrames. It can also handle single strings and infers formats automatically. Timezone-aware parsing is supported, making it ideal for data analysis workflows and processing CSV or Excel data.
Python
import pandas as pd
s = '2021-09-01 15:27:05.004573 +0530'
res = pd.to_datetime(s)
print(res)
Output2021-09-01 15:27:05.004573+05:30
Explanation: pd.to_datetime(s) infers the format of the date-time string s and converts it into a timezone-aware datetime object, suitable for efficient use in data analysis workflows.
Using arrow.get()
arrow.get() from the modern arrow library provides a clean and intuitive API for parsing and manipulating datetime objects. It handles timezones automatically and does not require an explicit format string.
Python
import arrow
s = '2021-09-01 15:27:05.004573 +0530'
res = arrow.get(s)
print(res)
Output2021-09-01 15:27:05.004573+05:30
Explanation: arrow.get(s) parses the date-time string s into an Arrow datetime object with timezone support, without requiring an explicit format string, providing a clean and user-friendly API.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice