Getting Now

from datetime import datetime, date, timedelta
now = datetime.now()
today = date.today()

Formatting Dates

print(now.strftime("%Y-%m-%d %H:%M"))
# 2026-04-22 14:05

Common directives: %Y year, %m month, %d day, %H:%M:%S time, %A weekday name.

Parsing Strings

d = datetime.strptime("2026-07-15", "%Y-%m-%d")

Arithmetic With timedelta

exam = date(2026, 4, 25)
days_left = (exam - today).days
print(f"{days_left} days to HKDSE")

one_week = timedelta(days=7)
next_week = today + one_week

Time Zones

from datetime import timezone
utc_now = datetime.now(timezone.utc)

Practise this on PyForm — free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm →