all Python 3datetimeterm
datetime.datetime
date and time
A moment in time: date plus time; supports arithmetic with timedelta and strftime().
Syntax
datetime.datetime(year, month, day, hour=0, minute=0, second=0)
datetime.now() hands back a naive object with no tzinfo, and Python refuses to subtract naive and aware values or order them with < and > — that is the surprise TypeError, while == between them silently gives False. For UTC use datetime.now(timezone.utc); the old utcnow() has been deprecated since Python 3.12. Instances are immutable, so changing one field means building a copy with replace().
Examples
import datetime
dt = datetime.datetime(2024, 1, 15, 10, 30)
print(dt) # → 2024-01-15 10:30:00
print(dt.strftime('%d.%m.%Y')) # → 15.01.2024
See also
Open in the full glossarydocs.python.orgReport an inaccuracy