What are Python modules for date manipulation?

Python provides several powerful modules for date and time manipulation, each serving different purposes. Here are the most important modules available for working with dates and times in Python.

Standard Library Modules

datetime Module

The datetime module is the primary built-in module for date and time manipulation. It provides classes for working with dates, times, and time intervals ?

from datetime import datetime, date, time, timedelta

# Current date and time
now = datetime.now()
print("Current datetime:", now)

# Create specific date
birthday = date(2024, 12, 25)
print("Date:", birthday)

# Date arithmetic
future_date = now + timedelta(days=30)
print("30 days from now:", future_date.strftime("%Y-%m-%d"))
Current datetime: 2024-01-15 14:30:25.123456
Date: 2024-12-25
30 days from now: 2024-02-14

time Module

The time module provides time-related functions, primarily working with timestamps and system time ?

import time

# Current timestamp
timestamp = time.time()
print("Timestamp:", timestamp)

# Convert timestamp to readable format
readable = time.ctime(timestamp)
print("Readable time:", readable)

# Format time
formatted = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print("Formatted time:", formatted)
Timestamp: 1705327825.123456
Readable time: Mon Jan 15 14:30:25 2024
Formatted time: 2024-01-15 14:30:25

Third-Party Modules

dateutil Module

The dateutil module extends the standard datetime module with powerful parsing and manipulation capabilities ?

# Note: Install with pip install python-dateutil
from dateutil import parser, relativedelta
from datetime import datetime

# Parse various date formats
date1 = parser.parse("2024-01-15")
date2 = parser.parse("Jan 15, 2024")
date3 = parser.parse("15/01/2024", dayfirst=True)

print("Parsed dates:")
print(date1.date())
print(date2.date())
print(date3.date())

# Relative date calculations
future = datetime.now() + relativedelta(months=3, days=10)
print("3 months and 10 days later:", future.date())
Parsed dates:
2024-01-15
2024-01-15
2024-01-15
3 months and 10 days later: 2024-04-25

pytz Module

The pytz module provides timezone support using the Olson timezone database ?

# Note: Install with pip install pytz
import pytz
from datetime import datetime

# Create timezone-aware datetime
utc = pytz.UTC
eastern = pytz.timezone('US/Eastern')
tokyo = pytz.timezone('Asia/Tokyo')

# Current time in different timezones
now_utc = datetime.now(utc)
now_eastern = now_utc.astimezone(eastern)
now_tokyo = now_utc.astimezone(tokyo)

print("UTC:", now_utc.strftime("%H:%M:%S %Z"))
print("Eastern:", now_eastern.strftime("%H:%M:%S %Z"))
print("Tokyo:", now_tokyo.strftime("%H:%M:%S %Z"))
UTC: 14:30:25 UTC
Eastern: 09:30:25 EST
Tokyo: 23:30:25 JST

Module Comparison

Module Type Best For Key Features
datetime Built-in Basic date/time operations Date arithmetic, formatting
time Built-in System time, timestamps Performance timing, sleep
dateutil Third-party Flexible parsing Parse any date format
pytz Third-party Timezone handling Accurate timezone conversion

Conclusion

For basic date operations, use the built-in datetime module. For timezone-aware applications, combine datetime with pytz. Use dateutil when you need to parse dates from various string formats.

Updated on: 2026-03-24T19:31:53+05:30

319 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements