
How to Convert String to Datetime in Python: strptime and dateutil Guide
When developing Python applications, you frequently extract dates and timestamps from external APIs, CSV files, or database queries. These values typically arrive as plain string objects (such as "2026-06-16 14:30:00").
To perform date math operations, comparisons, or formatting, you must parse these strings into Python datetime objects.
In this guide, we will analyze Python's built-in strptime method, explore the automated dateutil parser library, and configure timezone settings.
1. Using Python's Built-in datetime.strptime
The standard way to convert a string to a datetime object without installing third-party packages is using the strptime (string parse time) method from the built-in datetime module.
from datetime import datetime
date_string = "2026-06-16 14:30:00"
# Convert using format template
date_object = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print(type(date_object)) # Outputs: <class 'datetime.datetime'>
print(date_object) # Outputs: 2026-06-16 14:30:00Common Formatting Directives Reference
To parse a date correctly, your format string must match the pattern of the input date string exactly. Refer to these common directives:
%Y: Year with century as a decimal number (e.g.,2026).%m: Month as a zero-padded decimal number (e.g.,06).%d: Day of the month as a zero-padded decimal number (e.g.,16).%H: Hour (24-hour clock) as a zero-padded decimal number (e.g.,14).%M: Minute as a zero-padded decimal number (e.g.,30).%S: Second as a zero-padded decimal number (e.g.,00).%b: Abbreviated month name (e.g.,Jun).
If the input string does not match the format template exactly (e.g., a missing padding zero or an extra space), the method raises a ValueError:
# Raises ValueError: time data '2026-6-16' does not match format '%Y-%m-%d'
date_object = datetime.strptime("2026-6-16", "%Y-%m-%d") 2. Using dateutil.parser (Automatic Deduction)
If you must parse date strings arriving in varying formats, writing templates for each is tedious.
The third-party python-dateutil library parses almost any common date string format automatically without requiring a format string.
First, install the library:
pip install python-dateutilThen, parse date strings using the parse method:
from dateutil import parser
# dateutil automatically parses different date formats:
d1 = parser.parse("2026-06-16T14:30:00Z")
d2 = parser.parse("June 16, 2026, 2:30 PM")
d3 = parser.parse("16-06-2026 14:30")
print(d1) # Outputs: 2026-06-16 14:30:00+00:00
print(d2) # Outputs: 2026-06-16 14:30:00
print(d3) # Outputs: 2026-06-16 14:30:003. Handling Timezones (Naive vs. Aware Datetimes)
By default, dates parsed with strptime are naive datetime objects. They contain no information about what timezone they belong to, which can lead to timezone-offset bugs.
To parse dates as aware datetime objects (anchoring them to specific zones), pass timezone metadata.
In modern Python (v3.9+), utilize the built-in timezone classes or zoneinfo module:
from datetime import datetime, timezone
# Option A: Parse UTC offset directly using %z
date_str = "2026-06-16 14:30:00 +0000"
aware_dt = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S %z")
print(aware_dt.tzinfo) # Outputs: UTC
# Option B: Attach UTC timezone to a naive datetime manually
naive_dt = datetime.strptime("2026-06-16 14:30:00", "%Y-%m-%d %H:%M:%S")
utc_dt = naive_dt.replace(tzinfo=timezone.utc)
print(utc_dt.tzinfo) # Outputs: UTCConclusion
Converting date strings in Python can be managed through built-in standard tools or dedicated parser packages. Use datetime.strptime for projects where input formats are fixed, preventing external library dependencies. For dynamic inputs, implement dateutil.parser to parse dates automatically, and always attach timezone references to prevent localized system time calculations discrepancies.