Dates in Python are handled primarily through the built-in datetime module, which provides classes for working with dates, times, datetimes, time intervals, and time zones. The core classes — date, time, datetime, timedelta, and tzinfo — offer powerful, flexible tools for date arithmetic, formatting, parsing, comparisons, and timezone-aware calculations. In 2026, mastering datetime (and its companions like zoneinfo for modern time zones and pendulum or arrow for convenience) is essential for data analysis, logging, scheduling, APIs, financial apps, machine learning timestamps, and any code that deals with time-sensitive data.
Here’s a complete, practical guide to working with dates in Python: core classes, creation and parsing, formatting, arithmetic, comparisons, time zones, real-world patterns, and modern best practices with type hints and scalability.
The date class handles year-month-day values — no time or timezone.
from datetime import date, datetime, timedelta
today = date.today()
print(today) # 2026-02-10 (current date)
print(today.year, today.month, today.day) # 2026 2 10
# Create specific date
birthday = date(1990, 5, 15)
print(birthday) # 1990-05-15
The datetime class combines date and time — year, month, day, hour, minute, second, microsecond — and supports time zone info.
now = datetime.now()
print(now) # 2026-02-10 14:30:45.123456 (current datetime)
# Specific datetime
meeting = datetime(2026, 3, 1, 9, 0, 0)
print(meeting) # 2026-03-01 09:00:00
Parsing strings into dates/datetimes uses strptime() with format codes — very common for API responses, logs, CSVs.
date_str = "2026-02-10"
dt = datetime.strptime(date_str, "%Y-%m-%d")
print(dt) # 2026-02-10 00:00:00
log_entry = "2026-02-10 14:30:45"
log_dt = datetime.strptime(log_entry, "%Y-%m-%d %H:%M:%S")
print(log_dt) # 2026-02-10 14:30:45
Formatting dates/datetimes to strings uses strftime() — flexible format codes for any output style.
print(today.strftime("%A, %B %d, %Y")) # Tuesday, February 10, 2026
print(now.strftime("%Y-%m-%d %H:%M:%S")) # 2026-02-10 14:30:45
print(now.strftime("%b %d, %Y %I:%M %p")) # Feb 10, 2026 02:30 PM
Date arithmetic with timedelta — add/subtract days, hours, minutes, etc.
tomorrow = today + timedelta(days=1)
print(tomorrow) # 2026-02-11
one_week_ago = today - timedelta(weeks=1)
print(one_week_ago) # 2026-02-03
future_meeting = meeting + timedelta(hours=2, minutes=30)
print(future_meeting) # 2026-03-01 11:30:00
Comparisons are natural — datetime and date objects are comparable.
if tomorrow > today:
print("Tomorrow is later!") # True
if meeting < datetime.now():
print("Meeting already passed!")
Time zones with zoneinfo (Python 3.9+) — modern, built-in replacement for pytz.
from zoneinfo import ZoneInfo
ny_time = datetime.now(ZoneInfo("America/New_York"))
london_time = datetime.now(ZoneInfo("Europe/London"))
print(ny_time) # 2026-02-10 14:30:45-05:00 (example)
print(london_time) # 2026-02-10 19:30:45+00:00
Best practices make date handling safe, readable, and performant. Use datetime.now(tz=ZoneInfo("UTC")) for UTC timestamps in production — avoids local time surprises. Always parse with explicit format in strptime() — avoid dateutil.parser in production for consistency. Prefer strftime() with standard codes — ISO 8601 (%Y-%m-%dT%H:%M:%S) for interoperability. Modern tip: use Polars for large timestamp data — pl.col("date").str.to_datetime() or pl.col("date").dt.offset_by("1d") is 10–100× faster than pandas loops. Add type hints — date, datetime, timedelta — improves readability and mypy checks. Handle edge cases — leap years, DST transitions, invalid dates — use try/except ValueError when parsing. Use pendulum or arrow for human-friendly APIs — pendulum.now().add(days=1) is often clearer than timedelta. Avoid naive datetimes in production — always attach timezone for correct arithmetic and comparisons.
Dates in Python with datetime are powerful and flexible — create, parse, format, calculate, compare, and handle time zones reliably. In 2026, use UTC everywhere, explicit formats, type hints, and Polars for scale. Master dates, and you’ll handle timestamps, scheduling, logs, and analytics with confidence and correctness.
Next time you see a date or timestamp — reach for datetime. It’s Python’s cleanest way to work with time — accurately and efficiently.