Formatting datetime in Python is essential for displaying dates and times in human-readable, locale-specific, or machine-parsable ways — whether for logs, reports, user interfaces, API responses, file names, or data exports. The strftime() method (string format time) on date or datetime objects converts them to strings using flexible format codes like %Y (year), %m (month), %d (day), %H (hour), %M (minute), and many more. In 2026, strftime() remains the standard for custom datetime formatting — it’s fast, locale-aware (with proper setup), and works seamlessly with pandas/Polars for vectorized formatting of large timestamp columns.
Here’s a complete, practical guide to formatting datetime objects: core format codes, common patterns with examples, time zone and locale support, pandas/Polars vectorization, real-world use cases, and modern best practices for readability, interoperability, and performance.
The strftime() method takes a format string and returns the datetime as formatted text — codes start with % and represent specific components.
from datetime import datetime
now = datetime.now()
# Basic ISO-like datetime
print(now.strftime("%Y-%m-%d %H:%M:%S")) # e.g., 2026-02-10 14:30:45
# Human-readable with full names
print(now.strftime("%A, %B %d, %Y at %I:%M %p")) # e.g., Tuesday, February 10, 2026 at 02:30 PM
# Short numeric with time
print(now.strftime("%m/%d/%y %H:%M")) # e.g., 02/10/26 14:30
Common format codes and combinations — mix date, time, weekday, and timezone elements as needed.
dt = datetime(2026, 2, 10, 14, 30, 45)
print(dt.strftime("%Y-%m-%dT%H:%M:%S")) # 2026-02-10T14:30:45 (ISO 8601 basic)
print(dt.strftime("%Y-%m-%d %H:%M:%S")) # 2026-02-10 14:30:45
print(dt.strftime("%Y/%m/%d %I:%M:%S %p")) # 2026/02/10 02:30:45 PM
print(dt.strftime("%A %b %d %Y")) # Tuesday Feb 10 2026
print(dt.strftime("%c")) # Tue Feb 10 14:30:45 2026 (locale default)
print(dt.strftime("%x %X")) # 02/10/26 14:30:45 (locale date + time)
Timezone-aware formatting — include offset or Z for UTC using %z or %Z.
from zoneinfo import ZoneInfo
utc_now = datetime.now(ZoneInfo("UTC"))
print(utc_now.strftime("%Y-%m-%dT%H:%M:%S%z")) # e.g., 2026-02-10T19:30:45+0000
ny_now = datetime.now(ZoneInfo("America/New_York"))
print(ny_now.strftime("%Y-%m-%d %H:%M:%S %Z")) # e.g., 2026-02-10 14:30:45 EST
Real-world pattern: formatting timestamp columns in pandas — vectorized dt.strftime() turns datetime Series into formatted strings for logs, reports, or exports.
import pandas as pd
df = pd.DataFrame({
'event_time': [datetime(2026, 1, 15, 9, 0), datetime(2026, 2, 10, 14, 30)]
})
# Vectorized formatting
df['iso_timestamp'] = df['event_time'].dt.strftime('%Y-%m-%dT%H:%M:%S')
df['friendly'] = df['event_time'].dt.strftime('%A %I:%M %p, %b %d')
print(df)
# event_time iso_timestamp friendly
# 0 2026-01-15 09:00:00 2026-01-15T09:00:00 Thursday 09:00 AM, Jan 15
# 1 2026-02-10 14:30:00 2026-02-10T14:30:00 Tuesday 02:30 PM, Feb 10
Best practices make datetime formatting reliable and performant. Use ISO 8601 (%Y-%m-%dT%H:%M:%S) for machine-readable output — always sortable and parsable. Include timezone with %z or %Z in production — avoid naive datetimes. Modern tip: use Polars for large timestamp columns — pl.col("ts").dt.strftime("%Y-%m-%d %H:%M:%S") is 10–100× faster than pandas dt accessor. Add type hints — datetime.datetime — improves readability and mypy checks. Handle time zones with zoneinfo — dt.astimezone(ZoneInfo("UTC")).strftime(...) formats correctly with offset. For microsecond precision, include %f — truncate if not needed. Wrap formatting in try/except — invalid datetimes raise ValueError. Use strftime() with date for date-only, datetime for full timestamp — prevents accidental time inclusion. Combine with pd.to_datetime() or Polars parsing for input ? formatted output pipelines.
Formatting datetime with strftime() gives you complete control over date+time strings — ISO for machines, verbose for humans, custom for reports. In 2026, vectorize in pandas/Polars, include timezone info, use standard codes, and add type hints for safety. Master datetime formatting, and you’ll display, export, and log dates cleanly and consistently — readable by users and systems alike.
Next time you need to print or export a datetime — reach for strftime(). It’s Python’s cleanest way to say: “Show this moment exactly how I want it.”