Loading...
Loading...
Every pattern you'd Google - datetime, formatting, math, and more
Quick reference for all Python datetime format directives with examples and common patterns.
from datetime import datetime
dt = datetime(2026, 3, 25, 14, 30, 45)
# Date codes
dt.strftime("%Y") # "2026" (4-digit year)
dt.strftime("%y") # "26" (2-digit year)
dt.strftime("%m") # "03" (zero-padded month)
dt.strftime("%-m") # "3" (no padding, Linux/macOS)
dt.strftime("%d") # "25" (zero-padded day)
dt.strftime("%-d") # "25" (no padding, Linux/macOS)
dt.strftime("%B") # "March" (full month name)
dt.strftime("%b") # "Mar" (abbreviated month)
dt.strftime("%A") # "Wednesday" (full weekday)
dt.strftime("%a") # "Wed" (abbreviated weekday)
dt.strftime("%j") # "084" (day of year, zero-padded)
dt.strftime("%U") # "12" (week number, Sunday start)
dt.strftime("%W") # "12" (week number, Monday start)# Time codes
dt.strftime("%H") # "14" (24-hour, zero-padded)
dt.strftime("%I") # "02" (12-hour, zero-padded)
dt.strftime("%M") # "30" (minutes, zero-padded)
dt.strftime("%S") # "45" (seconds, zero-padded)
dt.strftime("%p") # "PM" (AM/PM)
dt.strftime("%f") # "000000" (microseconds, zero-padded)
dt.strftime("%Z") # "" (timezone name, empty if naive)
dt.strftime("%z") # "" (UTC offset, empty if naive)# Common patterns
dt.strftime("%Y-%m-%d") # "2026-03-25" (ISO date)
dt.strftime("%m/%d/%Y") # "03/25/2026" (US date)
dt.strftime("%B %d, %Y") # "March 25, 2026"
dt.strftime("%I:%M %p") # "02:30 PM"
dt.strftime("%H:%M:%S") # "14:30:45" (24-hour time)
dt.strftime("%Y-%m-%dT%H:%M:%S") # "2026-03-25T14:30:45" (ISO 8601)
dt.strftime("%A, %B %d, %Y %I:%M %p") # "Wednesday, March 25, 2026 02:30 PM"
# Parsing strings back to datetime
datetime.strptime("2026-03-25", "%Y-%m-%d")
datetime.strptime("03/25/2026 02:30 PM", "%m/%d/%Y %I:%M %p")
datetime.fromisoformat("2026-03-25T14:30:45") # Python 3.7+