🐍 Python 2026 › Lesson 20: Working with Dates and Times
Lesson 20 of 30

Working with Dates and Times

The datetime module: parsing, formatting, arithmetic, and timezones.

The datetime Module

from datetime import datetime, date, timedelta

now = datetime.now()
print(now)
print(now.year, now.month, now.day)
print(now.strftime("%d/%m/%Y %H:%M"))

today = date.today()
print(today)

Formatting Dates

CodeMeaningExample
%Y4-digit year2026
%mMonth (01–12)03
%dDay (01–31)05
%HHour 24h14
%MMinute30
%AFull weekdayThursday

Parsing Dates

dt = datetime.strptime("25/12/2026", "%d/%m/%Y")
print(dt)  # 2026-12-25 00:00:00

Date Arithmetic with timedelta

today = date.today()
deadline = today + timedelta(days=30)
print(f"Deadline: {deadline}")

diff = date(2026, 12, 31) - today
print(f"Days until New Year: {diff.days}")