strftime Format Codes in Python – Complete Guide for Data Science 2026
The strftime() method is the most powerful and flexible way to turn Python date and datetime objects into formatted strings. In data science, it is used constantly for generating readable reports, creating file names, building log entries, and preparing features for modeling. Mastering format codes lets you control exactly how dates and times appear in your outputs.
TL;DR — Most Useful strftime Codes
%Y→ 4-digit year (2026)%m→ 2-digit month (03)%d→ 2-digit day (19)%A→ Full weekday name (Thursday)%H:%M:%S→ Time in 24-hour format%Y-%m-%d→ Most common date format
1. Basic strftime Usage
from datetime import date, datetime
from zoneinfo import ZoneInfo
d = date(2026, 3, 19)
dt = datetime.now(ZoneInfo("UTC"))
print(d.strftime("%Y-%m-%d")) # 2026-03-19
print(d.strftime("%A, %B %d, %Y")) # Thursday, March 19, 2026
print(dt.strftime("%Y-%m-%d %H:%M:%S")) # 2026-03-19 14:30:25
print(dt.strftime("%A %d %B %Y at %I:%M %p")) # Thursday 19 March 2026 at 02:30 PM
2. pandas – Vectorized strftime for Large Datasets
import pandas as pd
df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])
df["report_date"] = df["order_date"].dt.strftime("%B %Y") # March 2026
df["file_date"] = df["order_date"].dt.strftime("%Y%m%d") # 20260319
df["full_date"] = df["order_date"].dt.strftime("%A, %B %d, %Y")
print(df[["order_date", "report_date", "full_date"]].head())
3. Real-World Data Science Examples
# Example 1: Generate daily report filenames
today_str = date.today().strftime("%Y%m%d")
filename = f"sales_report_{today_str}.csv"
# Example 2: Create human-readable log timestamps
log_time = datetime.now(ZoneInfo("UTC")).strftime("%Y-%m-%d %H:%M:%S UTC")
# Example 3: Build time-based categorical features
df["month_year"] = df["order_date"].dt.strftime("%Y-%m")
df["day_name"] = df["order_date"].dt.strftime("%A")
4. Best Practices in 2026
- Use
strftime()for custom, human-readable formats - Use
isoformat()when you need machine-readable output - Apply pandas
.dt.strftime()for large-scale formatting - Always format timezone-aware datetimes consistently
- Define format constants at the top of your scripts for consistency
Conclusion
The strftime() method is the go-to tool for turning dates and times into any string format you need. In 2026 data science workflows, mastering common format codes and using pandas vectorized methods makes report generation, file naming, logging, and feature engineering fast, clean, and professional.
Next steps:
- Add formatted date/time string columns to your current datasets using
strftime()or.dt.strftime()