🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

datetime Module

AI & DATA SCIENCE // datetime-module

The datetime module provides classes for representing and manipulating dates, times, and the intervals between them.

Syntax

from datetime import datetime, date, timedelta
datetime.now()
date(2026, 7, 23)
timedelta(days=7)

Deep Dive Course

The module's main classes are date, a calendar date with no time; time, a time of day with no date; datetime, a combination of both; and timedelta, a duration, the difference between two dates or datetimes. datetime objects can be naive, with no timezone information, or timezone-aware; naive datetimes are simpler to work with but can't be safely compared across different timezones, which is a common source of bugs in date-handling code. Formatting a datetime as text uses strftime, and parsing text into a datetime uses strptime, both driven by the same format-code mini-language.

1Understanding datetime Module

The module's main classes are date, a calendar date with no time; time, a time of day with no date; datetime, a combination of both; and timedelta, a duration, the difference between two dates or datetimes. datetime objects can be naive, with no timezone information, or timezone-aware; naive datetimes are simpler to work with but can't be safely compared across different timezones, which is a common source of bugs in date-handling code. Formatting a datetime as text uses strftime, and parsing text into a datetime uses strptime, both driven by the same format-code mini-language.

💡

Prefer timezone-aware datetimes for anything that will be compared, stored, or displayed across different timezones — naive datetimes silently assume 'whatever timezone the reader has in mind', which causes subtle bugs when servers, users, and databases don't all agree.

editor.html
from datetime import date

today = date(2026, 7, 23)
print(today)
print(today.strftime("%B %d, %Y"))
localhost:3000

2Practical Example

Here is a real-world application of datetime Module showing how it is used in production Python code.

editor.html
from datetime import date, timedelta

today = date(2026, 7, 23)
next_week = today + timedelta(days=7)
print(next_week)
localhost:3000

3Best Practices

Follow these guidelines when working with datetime Module:

1. Use timedelta for date/time arithmetic, adding or subtracting durations, instead of manually calculating with seconds or days

2. Store and pass around timezone-aware datetimes in backend/server code, converting to a specific timezone only for display

3. Use strftime/strptime with explicit format strings for converting between datetime objects and text, rather than manual string manipulation

⚠️

Tip: Prefer timezone-aware datetimes for anything that will be compared, stored, or displayed across different timezones — naive datetimes silently assume 'whatever timezone the reader has in mind', which causes subtle bugs when servers, users, and databases don't all agree.

editor.html
from datetime import date

today = date(2026, 7, 23)
print(today)
print(today.strftime("%B %d, %Y"))
localhost:3000

Examples

Example 01Basic Usage
from datetime import date

today = date(2026, 7, 23)
print(today)
print(today.strftime("%B %d, %Y"))
Example 02Advanced Example
from datetime import date, timedelta

today = date(2026, 7, 23)
next_week = today + timedelta(days=7)
print(next_week)

Best Practices

  • Use timedelta for date/time arithmetic, adding or subtracting durations, instead of manually calculating with seconds or days
  • Store and pass around timezone-aware datetimes in backend/server code, converting to a specific timezone only for display
  • Use strftime/strptime with explicit format strings for converting between datetime objects and text, rather than manual string manipulation

Interview Question

What's the difference between a naive and a timezone-aware datetime object, and why does mixing them cause errors?

Hint: Think about what information each one is missing.

A naive datetime carries no timezone information at all — it's just a date and time with no context about which timezone it applies to. A timezone-aware datetime additionally stores an offset, or timezone rule, describing exactly where it is on the global timeline. Python explicitly refuses to compare or subtract a naive datetime with a timezone-aware one, raising a TypeError, because doing so would require guessing which timezone the naive one is supposed to represent, which could silently produce a wrong result if Python guessed incorrectly.

Exercises

MediumPractice using datetime Module in a real scenario.
View Solution
from datetime import date

today = date(2026, 7, 23)
print(today)
print(today.strftime("%B %d, %Y"))

Frequently Asked Questions

What's the difference between a naive and a timezone-aware datetime object, and why does mixing them cause errors?

A naive datetime carries no timezone information at all — it's just a date and time with no context about which timezone it applies to. A timezone-aware datetime additionally stores an offset, or timezone rule, describing exactly where it is on the global timeline. Python explicitly refuses to compare or subtract a naive datetime with a timezone-aware one, raising a TypeError, because doing so would require guessing which timezone the naive one is supposed to represent, which could silently produce a wrong result if Python guessed incorrectly.

Related Functions

math-moduleos-moduleas-keyword