
from datetime import datetime, date, timedelta # Current time now = datetime.now() today = date.today() print(now.year, now.month, now.day) print(now.hour, now.minute, now.second)
Creating dates:
birthday = date(1990, 6, 15) event = datetime(2024, 12, 25, 9, 0, 0)
Arithmetic:
one_week = timedelta(days=7) next_week = today + one_week yesterday = today - timedelta(days=1) diff = date(2025, 1, 1) - today print(diff.days) # days until New Year
Formatting & parsing:
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
# "2024-03-15 14:30:00"
parsed = datetime.strptime("2024-03-15", "%Y-%m-%d")Common format codes: %Y=year, %m=month, %d=day, %H=hour(24), %I=hour(12), %M=minute, %S=second, %A=weekday name.
Reference:
TaskLoco™ — The Sticky Note GOAT