Last modified: May 04, 2026 By Alexander Williams

Master Python Datetime Guide

Python's datetime module is essential for handling dates and times. It helps you parse, format, and manipulate time data. This guide covers everything you need. We will use simple examples and code. You will learn to work with dates, times, and time zones.

What is Python Datetime?

The datetime module provides classes for dates and times. It includes date, time, datetime, and timedelta. These classes make time calculations easy. You can create, compare, and modify dates. This module is part of Python's standard library. No extra installation is needed.

Getting Current Date and Time

Use datetime.datetime.now() to get the current date and time. It returns a datetime object. You can access year, month, day, hour, minute, second, and microsecond.

 
from datetime import datetime

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

# Access individual components
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour)

Current datetime: 2025-03-15 10:30:45.123456
Year: 2025
Month: 3
Day: 15
Hour: 10

Creating Specific Dates

You can create a datetime object for any date. Pass year, month, day, hour, minute, second, and microsecond. Only year, month, and day are required.

 
from datetime import datetime

# Create a specific date
my_date = datetime(2024, 12, 25, 14, 30, 0)
print("Christmas:", my_date)

# Create a date only (without time)
from datetime import date
birthday = date(1990, 5, 15)
print("Birthday:", birthday)

Christmas: 2024-12-25 14:30:00
Birthday: 1990-05-15

Formatting Dates with strftime

Use strftime() to format a datetime object into a string. It uses format codes like %Y for year and %m for month. This is useful for displaying dates in a human-readable way.

 
from datetime import datetime

now = datetime.now()
# Format as "Month Day, Year"
formatted = now.strftime("%B %d, %Y")
print("Formatted date:", formatted)

# Format as "HH:MM AM/PM"
time_formatted = now.strftime("%I:%M %p")
print("Formatted time:", time_formatted)

Formatted date: March 15, 2025
Formatted time: 10:30 AM

Parsing Dates with strptime

Use strptime() to parse a string into a datetime object. You must specify the format of the input string. This is helpful when reading dates from files or user input.

 
from datetime import datetime

# Parse a date string
date_string = "2025-03-15 14:30:00"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print("Parsed datetime:", parsed_date)

# Parse a different format
another_string = "15/03/2025"
parsed_another = datetime.strptime(another_string, "%d/%m/%Y")
print("Parsed date:", parsed_another)

Parsed datetime: 2025-03-15 14:30:00
Parsed date: 2025-03-15

Date Arithmetic with timedelta

timedelta represents a duration. You can add or subtract it from a datetime object. This is great for calculating future or past dates.

 
from datetime import datetime, timedelta

today = datetime.now()
# Add 10 days
future_date = today + timedelta(days=10)
print("10 days from now:", future_date)

# Subtract 5 hours
past_time = today - timedelta(hours=5)
print("5 hours ago:", past_time)

# Difference between two dates
date1 = datetime(2025, 1, 1)
date2 = datetime(2025, 3, 15)
difference = date2 - date1
print("Days between:", difference.days)

10 days from now: 2025-03-25 10:30:45.123456
5 hours ago: 2025-03-15 05:30:45.123456
Days between: 73

Working with Time Zones

Python's datetime module supports time zones. Use the pytz library for accurate time zone handling. You can convert between time zones easily.

 
from datetime import datetime
import pytz

# Set time zone
utc = pytz.utc
now_utc = datetime.now(utc)
print("UTC time:", now_utc)

# Convert to another time zone
eastern = pytz.timezone("US/Eastern")
now_eastern = now_utc.astimezone(eastern)
print("Eastern time:", now_eastern)

# Create timezone-aware datetime
paris = pytz.timezone("Europe/Paris")
paris_time = datetime.now(paris)
print("Paris time:", paris_time)

UTC time: 2025-03-15 15:30:45.123456+00:00
Eastern time: 2025-03-15 11:30:45.123456-04:00
Paris time: 2025-03-15 16:30:45.123456+01:00

Comparing Dates

You can compare datetime objects using standard operators. Use <, >, ==, and !=. This is useful for checking deadlines or expiration dates.

 
from datetime import datetime

deadline = datetime(2025, 4, 1)
today = datetime.now()

if today < deadline:
    print("Still time to submit.")
elif today == deadline:
    print("Today is the deadline.")
else:
    print("Deadline has passed.")

Still time to submit.

Common Pitfalls and Tips

Always handle time zones explicitly to avoid bugs. Use pytz for reliable conversions. Remember that datetime.now() returns local time without time zone info. For UTC, use datetime.utcnow() or datetime.now(pytz.utc).

When parsing dates, match the format exactly. Use %Y for four-digit years and %y for two-digit years. Be careful with month and day order in different locales.

Conclusion

Python's datetime module is powerful yet easy to use. You learned to get current time, create dates, format strings, parse input, perform arithmetic, and handle time zones. Practice these examples to master date and time manipulation. For more advanced use, explore the calendar module and third-party libraries like dateutil. Happy coding!