Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

datetime

Python’s datetime module supplies standard-library types for dates, times, combined timestamps, durations, and time zones. It supports arithmetic, field extraction, conversion, and explicit string formatting.

How it works

The module provides date, time, combined datetime, duration, and timezone-related types. These objects support arithmetic, attribute access, conversion, and formatting for common backend and scripting tasks.

Syntax

import datetime

class datetime.date
class datetime.time
class datetime.datetime
class datetime.timedelta
class datetime.tzinfo
class datetime.timezone

Parameters

NameTypeDescription
formatstrFormat string controlling the textual representation.

Returns

The module contains temporal classes; individual constructors and methods return the objects or strings described by their respective documentation.

Key points

  • Use the module as the standard-library foundation for calendar and clock values.
  • date models an idealized Gregorian calendar date with year, month, and day attributes.
  • time stores clock fields independently of a calendar date.
  • timedelta represents date or datetime differences with microsecond-level resolution.
  • datetime is the combined calendar-and-clock type and exposes fields from both components.
  • Use strftime with an explicit format to turn supported temporal objects into display strings.

Common mistakes

  • The time model does not represent leap seconds, so calculations use ordinary fixed-length days.
  • A datetime combines calendar and clock fields; use date or time separately when the other component is irrelevant.

Example: Create an aware UTC timestamp

from datetime import datetime, timezone

now = datetime.now(timezone.utc)
print(now.isoformat())

Creates an aware UTC datetime and formats it as an ISO 8601 string.

When to use it

Calendar dates

Represent a calendar day without a clock component.

Timezone-aware timestamps

Record an instant with an explicit UTC offset.

Date and time arithmetic

Compute elapsed or scheduled intervals.

Controlled formatting

Render temporal values for logs, APIs, or reports.

Interview questions

QWhat does the datetime module provide?

The module supplies classes for dates, times, combined datetimes, durations, and timezone information. A datetime combines a date and a time, while timedelta represents a duration between date or datetime values.

Follow-up: How would you produce a UTC timestamp? Use an aware datetime with a UTC tzinfo, such as datetime.now(timezone.utc), rather than relying on a local naive value.

QHow do date, time, datetime, and timedelta differ?

date represents a calendar date, time represents a time independent of a particular day, and datetime combines both. timedelta expresses a duration at microsecond resolution.

Follow-up: Why might date be preferable to datetime? It avoids carrying an irrelevant clock and timezone component when only calendar-day operations are required.

QHow are datetime values formatted?

strftime(format) creates a formatted string under control of an explicit format string. The date, datetime, and time types support this method.

Follow-up: How can formatted text be parsed back? Use the corresponding strptime behavior with a format that describes the input representation.

  • datetime.date
  • datetime.time
  • datetime.datetime
  • datetime.timedelta
  • datetime.tzinfo
  • datetime.timezone
  • datetime.strftime
  • datetime.strptime

Source: datetime — Basic date and time types. Checked 2026-10-15.