Skip to content
Learn Netverks

Lesson

Step 27/36 75% through track

datetime-python

datetime module

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches datetime module: the syntax, patterns, and safety habits you need before advancing in Python.

Teams still ship datetime module in Python codebases—skipping it leaves gaps in debugging and code reviews.

You will apply datetime module in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

The datetime module models dates, times, and timedeltas. Always store UTC in backends and convert for display—critical before timezone-aware Django models in the Django track.

date, datetime, timedelta

from datetime import date, datetime, timedelta, timezone

today = date.today()
now = datetime.now(timezone.utc)
later = now + timedelta(days=7)

Formatting and parsing

now.strftime("%Y-%m-%d")
datetime.strptime("2026-05-28", "%Y-%m-%d")

Prefer timezone-aware datetime objects for APIs—avoid naive local times in persisted data.

Important interview questions and answers

  1. Q: Naive vs aware datetime?
    A: Aware datetimes include tzinfo; naive have no timezone—ambiguous for distributed systems.
  2. Q: Why UTC in storage?
    A: Single canonical timeline—convert to user locale at the edge.

Self-check

  1. What adds seven days to a datetime?
  2. Why avoid naive datetimes in APIs?

Pitfall: Naive datetimes cause timezone bugs—store UTC, convert for display; Django expands this in Django.

Interview prep

Naive vs aware?

Aware includes tzinfo; naive lacks timezone—ambiguous for distributed systems.

Why UTC storage?

Single timeline—convert to local zones at display layer.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • timezone aware?
  • strftime use?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump