LESSON · 1 OF 5

The datetime module

The datetime module

Working with time in Python starts with the datetime module. It gives you a datetime type for a full moment (date plus time) and a date type for a calendar day on its own.

python
from datetime import datetime, date

The current moment

datetime.now() returns the current local date and time as a datetime object:

python
now = datetime.now()
print(now)   # 2026-07-24 14:03:51.412000

date.today() gives just today's calendar date:

python
date.today()   # 2026-07-24

Building a specific moment

Pass year, month, and day (and optionally hour, minute, second) to construct an exact value:

python
released = datetime(2026, 3, 1, 9, 30, 0)
deadline = date(2026, 7, 30)

Reading the parts

Every datetime and date exposes its fields as attributes:

python
now = datetime.now()
now.year     # 2026
now.month    # 7
now.day      # 24
now.hour     # 14

These are plain integers, so you can compare them, print them, or feed them into other calculations.

What this node introduces

  • datetime.now() returns the current date and time.
  • date.today() returns the current calendar date.
  • datetime(y, m, d, ...) and date(y, m, d) build specific values.
  • .year, .month, .day, .hour read the parts.
Spin up a fresh environment and practice live.
python-devops · fresh machine · ready in under a minute