Convert python datetime to epoch with strftime
Categories:
Converting Python datetime to Epoch Timestamp with strftime

Learn how to accurately convert Python datetime objects, including UTC and local times, into Unix epoch timestamps using the strftime
method and timestamp()
.
Converting datetime
objects to Unix epoch timestamps is a common task in programming, especially when dealing with data serialization, API interactions, or cross-system compatibility. The Unix epoch, defined as January 1, 1970, 00:00:00 UTC, serves as a universal reference point for time. Python offers several ways to perform this conversion, and while strftime
is powerful for formatting, direct conversion to epoch often involves the timestamp()
method. This article will guide you through the process, highlighting best practices for handling timezones and ensuring accuracy.
Understanding Epoch Timestamps
An epoch timestamp (also known as Unix time or POSIX time) is the number of seconds that have elapsed since the Unix epoch. It's a simple, integer-based representation of a point in time, making it ideal for storage, comparison, and transfer across different systems and programming languages. It's crucial to remember that epoch timestamps are inherently UTC (Coordinated Universal Time) based. If you convert a local time datetime
object to an epoch without proper timezone handling, you might get an incorrect result relative to UTC.
flowchart TD A[Python datetime object] --> B{Is it timezone-aware?} B -->|No| C[Localize or Convert to UTC] B -->|Yes| D[Convert to UTC (if not already)] C --> D D --> E[Get timestamp using .timestamp()] E --> F[Epoch Timestamp (seconds since 1970-01-01 00:00:00 UTC)]
Workflow for converting a Python datetime object to an epoch timestamp, emphasizing timezone handling.
Converting Timezone-Naive datetime to Epoch
A datetime
object is 'naive' if it doesn't contain any timezone information. When converting a naive datetime
to an epoch, Python assumes it's in the system's local timezone. If your intention is to get a UTC epoch, you must first convert the naive datetime
to a UTC-aware datetime
.
import datetime
import pytz
# 1. Naive datetime (assumed local time)
naive_dt = datetime.datetime(2023, 10, 27, 10, 30, 0)
print(f"Naive datetime: {naive_dt}")
print(f"Epoch from naive (local assumption): {naive_dt.timestamp()}")
# 2. Convert naive to UTC-aware for correct epoch
# Get the local timezone (e.g., 'America/New_York')
local_tz = pytz.timezone('America/New_York') # Replace with your local timezone
# Localize the naive datetime
local_dt = local_tz.localize(naive_dt)
print(f"Localized datetime: {local_dt}")
# Convert localized datetime to UTC
utc_dt_from_local = local_dt.astimezone(pytz.utc)
print(f"UTC datetime from localized: {utc_dt_from_local}")
print(f"Correct epoch from localized: {utc_dt_from_local.timestamp()}")
# Alternative: Directly assume naive is UTC (if you know it is)
# This is less common but useful if your naive datetime is already UTC
naive_as_utc = naive_dt.replace(tzinfo=datetime.timezone.utc)
print(f"Naive datetime assumed as UTC: {naive_as_utc}")
print(f"Epoch from naive (assumed UTC): {naive_as_utc.timestamp()}")
Converting a timezone-naive datetime object to an epoch timestamp, demonstrating localization and UTC conversion.
datetime
object, Python's timestamp()
method will interpret it as local time. This can lead to incorrect epoch values if the original time was intended to be in UTC or a different timezone.Converting Timezone-Aware datetime to Epoch
When your datetime
object is already timezone-aware, the process is more straightforward. The timestamp()
method will correctly convert it to the epoch based on its inherent timezone information. For consistency and to avoid potential issues with daylight saving transitions, it's often best practice to convert any timezone-aware datetime
to UTC before getting its timestamp.
import datetime
import pytz
# Create a timezone-aware datetime object (e.g., in London time)
london_tz = pytz.timezone('Europe/London')
aware_dt_london = london_tz.localize(datetime.datetime(2023, 10, 27, 10, 30, 0))
print(f"Aware datetime (London): {aware_dt_london}")
print(f"Epoch from aware (London): {aware_dt_london.timestamp()}")
# Convert to UTC first for clarity and consistency
aware_dt_utc = aware_dt_london.astimezone(pytz.utc)
print(f"Aware datetime (UTC): {aware_dt_utc}")
print(f"Epoch from aware (UTC): {aware_dt_utc.timestamp()}")
# Using Python 3.2+ built-in timezone.utc
# Create a UTC-aware datetime directly
utc_dt_direct = datetime.datetime(2023, 10, 27, 10, 30, 0, tzinfo=datetime.timezone.utc)
print(f"Direct UTC datetime: {utc_dt_direct}")
print(f"Epoch from direct UTC: {utc_dt_direct.timestamp()}")
Converting timezone-aware datetime objects to epoch, showing direct conversion and conversion via UTC.
datetime.timezone.utc
over pytz.utc
if you only need UTC. pytz
is more robust for handling various non-UTC timezones and historical data.Why strftime
is not for direct Epoch Conversion
While the article title mentions strftime
, it's important to clarify its role. strftime
(string format time) is used to format a datetime
object into a human-readable string according to a specified format code. It can produce a string that looks like an epoch timestamp (e.g., using %s
), but this is generally not the recommended way to get a numeric epoch value in Python. The %s
format code for strftime
is not universally supported across all platforms and Python versions, and it returns a string, not a numeric type. The timestamp()
method is the canonical and most reliable way to get a numeric epoch value.
import datetime
import pytz
# Create a UTC-aware datetime
utc_dt = datetime.datetime(2023, 10, 27, 10, 30, 0, tzinfo=datetime.timezone.utc)
# Using .timestamp() (Recommended for numeric epoch)
epoch_numeric = utc_dt.timestamp()
print(f"Epoch (numeric) using .timestamp(): {epoch_numeric}")
print(f"Type of epoch_numeric: {type(epoch_numeric)}")
# Using strftime with %s (Not recommended for numeric epoch, returns string)
# Note: %s might not be available on all systems/Python versions (e.g., Windows)
try:
epoch_string = utc_dt.strftime('%s')
print(f"Epoch (string) using strftime('%s'): {epoch_string}")
print(f"Type of epoch_string: {type(epoch_string)}")
except ValueError as e:
print(f"strftime('%s') not supported on this platform: {e}")
Demonstrating the difference between timestamp()
and strftime('%s')
for epoch conversion.