Adding time to the mix with strftime() unlocks the full power of formatting datetime objects — combining date and time components into human-readable or machine-friendly strings using flexible format codes. While date-only formats like %Y-%m-%d are great for calendars and reports, including time (%H:%M:%S, %I:%M %p) is essential for logs, timestamps, event scheduling, API payloads, and user-facing displays. In 2026, mastering time-inclusive strftime() remains critical — it ensures consistent, parsable output for databases, JSON, CSV exports, notifications, and analytics dashboards, often combined with time zones and vectorized pandas/Polars operations for large datasets.
Here’s a complete, practical guide to adding time to strftime() formats: key time codes, common combined patterns with examples, real-world use cases, pandas/Polars integration, and modern best practices for readability, timezone handling, and performance.
Basic time codes include %H (24-hour, 00–23), %I (12-hour, 01–12), %M (minutes 00–59), %S (seconds 00–59), %p (AM/PM), and %f (microseconds). Combine them with date codes using any separators.
from datetime import datetime
dt = datetime(2026, 2, 10, 14, 30, 45, 123456)
# Full datetime (ISO-like)
print(dt.strftime("%Y-%m-%d %H:%M:%S")) # 2026-02-10 14:30:45
# 12-hour with AM/PM
print(dt.strftime("%Y/%m/%d %I:%M:%S %p")) # 2026/02/10 02:30:45 PM
# Verbose human-readable
print(dt.strftime("%A, %B %d, %Y at %I:%M %p")) # Tuesday, February 10, 2026 at 02:30 PM
# Short numeric with time
print(dt.strftime("%m/%d/%y %H:%M:%S")) # 02/10/26 14:30:45
Real-world pattern: formatting timestamps in pandas — vectorized dt.strftime() turns datetime columns into custom 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_time'] = df['event_time'].dt.strftime('%A %I:%M %p, %b %d')
print(df)
# event_time iso_timestamp friendly_time
# 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 time-inclusive formatting reliable and performant. Use %Y-%m-%dT%H:%M:%S for ISO 8601 datetime strings — universally parsable and sortable. Include timezone when relevant — dt.astimezone(ZoneInfo("UTC")).strftime('%Y-%m-%dT%H:%M:%S%z') adds offset (e.g., +0000). 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 — avoid naive datetimes in production. For microsecond precision, include %f — but 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.
Adding time to strftime() formats 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 time-inclusive formatting, and you’ll create consistent, parsable, readable timestamps across logs, APIs, dashboards, and exports.
Next time you need a date+time as a string — reach for strftime() with time codes. It’s Python’s cleanest way to say: “Show this moment, date and time, exactly as I want.”