Python Datetime Module Overview | Generated by AI

Home 2025.10

Overview of Python’s datetime Module

Python’s datetime module provides classes for manipulating dates, times, and timestamps. It’s in the standard library, so no installation is needed. Key classes include:

It’s useful for tasks like logging timestamps, calculating durations, or formatting dates for display/output.

Importing the Module

Import the entire module or specific classes as needed:

import datetime  # Full module

# Or import specific classes
from datetime import datetime, date, time, timedelta

Getting Current Date and Time

Use datetime.now() to get the current local date and time as a datetime object.

import datetime

now = datetime.datetime.now()
print(now)  # Output: e.g., 2023-10-05 14:30:45.123456
print(type(now))  # <class 'datetime.datetime'>

For UTC time, use datetime.utcnow() (though preferably use datetime.now(timezone.utc) with imports from datetime.timezone for timezone awareness).

Creating Date and Time Objects

Construct objects manually with their constructors.

# Date: year, month, day
d = datetime.date(2023, 10, 5)
print(d)  # 2023-10-05

# Time: hour, minute, second, microsecond (optional)
t = datetime.time(14, 30, 45)
print(t)  # 14:30:45

# Datetime: combines date and time
dt = datetime.datetime(2023, 10, 5, 14, 30, 45)
print(dt)  # 2023-10-05 14:30:45

Omit parts that aren’t needed (e.g., datetime.datetime(2023, 10, 5) creates a datetime at midnight).

Formatting Dates (strftime)

Convert dates to strings using strftime with format codes (e.g., %Y for year, %m for month).

now = datetime.datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)  # e.g., 2023-10-05 14:30:45

# Common formats:
# %A: Full weekday (e.g., Thursday)
# %B: Full month (e.g., October)
# %Y-%m-%d: ISO date

Parsing Dates from Strings (strptime)

Convert strings to datetime objects using strptime with matching formats.

date_str = "2023-10-05 14:30:45"
parsed = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(parsed)  # 2023-10-05 14:30:45
print(type(parsed))  # <class 'datetime.datetime'>

Match the format exactly, or it will raise a ValueError.

Date Arithmetic with timedelta

Add or subtract time intervals using timedelta.

now = datetime.datetime.now()
one_day = datetime.timedelta(days=1)
tomorrow = now + one_day
print(tomorrow)  # Current date + 1 day

# Subtract
yesterday = now - one_day

# Parts: days, seconds, microseconds, milliseconds, minutes, hours, weeks
one_week = datetime.timedelta(weeks=1)

For differences between dates:

date1 = datetime.datetime(2023, 10, 5)
date2 = datetime.datetime(2023, 10, 10)
delta = date2 - date1
print(delta.days)  # 5

Timezones (Advanced Usage)

For timezone-aware operations, use timezone (Python 3.3+). The module doesn’t handle timezone conversions natively; pair with pytz library for robust handling.

from datetime import datetime, timezone

# Naive datetime (no timezone)
naive = datetime.now()

# UTC aware
utc = datetime.now(timezone.utc)

# Custom timezone (requires tzinfo)
import pytz  # Might need pip install pytz
eastern = pytz.timezone("US/Eastern")
eastern_time = naive.astimezone(eastern)

Common Tips and Pitfalls

For full documentation, check Python’s official docs at docs.python.org. Practice with these examples in a Python interpreter.


Back

x-ai/grok-code-fast-1

Donate