Last modified: May 06, 2026 By Alexander Williams

Python Datetime Module Guide

Working with dates and times is a common task in programming. Python's datetime module makes it easy. It provides classes to handle dates, times, and time intervals. This guide will help you understand the core components. You will learn by example.

We will cover creating dates, formatting them, and parsing strings. We will also look at time zones and timestamps. This article is perfect for beginners. It uses short sentences and clear code. Let's dive in.

What is the Datetime Module?

The datetime module is part of Python's standard library. It means you don't need to install anything extra. It has several important classes. The most common ones are date, time, datetime, and timedelta. Each serves a specific purpose.

The date class represents a year, month, and day. The time class represents hours, minutes, seconds, and microseconds. The datetime class combines both date and time. The timedelta class represents a duration or difference between two dates or times.

Getting the Current Date and Time

To get the current date and time, use the datetime.now() method. It returns a datetime object. Let's see an example.

 
# Import the datetime class from the datetime module
from datetime import datetime

# Get the current date and time
now = datetime.now()
print(now)

2025-03-15 14:30:45.123456

You can also get just the current date or time separately. Use date.today() for the date. Use datetime.now().time() for the time. For more details, check out our Python datetime now: Get Current Date & Time guide.

Creating Specific Dates and Times

You can create your own dates and times. Pass the year, month, and day to the date constructor. For datetime, also pass hour, minute, second, and microsecond. All arguments are integers.

 
from datetime import date, datetime

# Create a specific date
my_date = date(2025, 12, 25)
print(my_date)

# Create a specific datetime
my_datetime = datetime(2025, 12, 25, 10, 30, 0)
print(my_datetime)

2025-12-25
2025-12-25 10:30:00

Formatting Dates with Strftime

The strftime method converts a datetime object to a string. You specify a format string. Format codes like %Y for year and %m for month control the output. This is very useful for displaying dates.

 
from datetime import datetime

now = datetime.now()

# Format as: Day-Month-Year
formatted = now.strftime("%d-%m-%Y")
print(formatted)

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

15-03-2025
March 15, 2025

There are many format codes available. They help you create any date string you need. For a complete list, see our Python Datetime Strftime Guide.

Parsing Strings with Strptime

The strptime method does the opposite of strftime. It parses a string into a datetime object. You must provide the format of the input string. This is essential for reading dates from files or user input.

 
from datetime import datetime

date_string = "25-12-2025"
# Parse using the same format as the string
parsed_date = datetime.strptime(date_string, "%d-%m-%Y")
print(parsed_date)

2025-12-25 00:00:00

Always match the format string exactly to your input. Otherwise, you will get an error. For more help, read our Python datetime strptime: Parse Strings to Dates article.

Working with Timedelta

The timedelta class represents a duration. You can add or subtract it from dates and times. This is perfect for calculating future or past dates. You can specify days, seconds, microseconds, and more.

 
from datetime import datetime, timedelta

now = datetime.now()
# Add 7 days to the current datetime
future_date = now + timedelta(days=7)
print(future_date)

# Subtract 2 hours
past_time = now - timedelta(hours=2)
print(past_time)

2025-03-22 14:30:45.123456
2025-03-15 12:30:45.123456

Working with Time Zones

Time zones can be tricky. Python's datetime module supports time zones through the timezone class. You can create a time zone with a fixed offset from UTC. For more complex needs, use the pytz library.

 
from datetime import datetime, timezone, timedelta

# Create a timezone for UTC+5:30
tz = timezone(timedelta(hours=5, minutes=30))

# Get current time in that timezone
now_tz = datetime.now(tz)
print(now_tz)

2025-03-15 20:00:45.123456+05:30

Time zones are important for global applications. Learn more in our Python Datetime Timezone Guide.

Using Timestamps

A timestamp is the number of seconds since January 1, 1970 (Unix epoch). The timestamp() method converts a datetime object to a timestamp. The fromtimestamp() method does the reverse. Timestamps are useful for storing dates in databases.

 
from datetime import datetime

now = datetime.now()
# Convert to timestamp
ts = now.timestamp()
print(ts)

# Convert back to datetime
dt = datetime.fromtimestamp(ts)
print(dt)

1742033445.123456
2025-03-15 14:30:45.123456

For a deeper dive into timestamps, check our Python Datetime Timestamp Explained guide.

Formatting Dates to Strings

We already saw strftime for formatting. But you can also use the isoformat() method. It returns a string in ISO 8601 format. This is a standard format for dates and times. It is often used in APIs and data exchange.

 
from datetime import datetime

now = datetime.now()
# Get ISO 8601 format
iso_string = now.isoformat()
print(iso_string)

2025-03-15T14:30:45.123456

For more details on ISO formatting, see our Python Datetime ISOFormat Guide.

Common Use Cases

The datetime module is used everywhere. Here are some common examples:

  • Logging: Add timestamps to log entries.
  • Scheduling: Calculate future dates for tasks.
  • Data Analysis: Parse and manipulate date columns.
  • Web Development: Handle user birthdays or event dates.

These are just a few examples. The module is versatile and powerful. You can combine it with other Python features easily.

Conclusion

The Python datetime module is essential for any developer. It provides simple classes for dates, times, and durations. You learned how to get the current time, create specific dates, and format them. You also learned about parsing strings, using timedelta, and working with time zones. The examples in this guide are ready to use in your projects. Practice with these concepts to master date and time manipulation in Python. For a complete reference, read our Master Python Datetime Guide.