Python datetime Module Overview
The datetime module helps you work with dates, times, and time differences in Python.
It is part of the Python standard library, so you do not need to install anything extra. Beginners often use it to get the current date, create a specific date, format a date as text, or add a number of days to a date.
Quick example
from datetime import datetime, date, time, timedelta
now = datetime.now()
today = date.today()
meeting = time(14, 30)
one_week = timedelta(days=7)
print(now)
print(today)
print(meeting)
print(now + one_week)
What this code does:
datetime.now()gets the current date and timedate.today()gets today's datetime(14, 30)creates a time value for 2:30 PMtimedelta(days=7)represents 7 daysnow + one_weekgives the date and time one week from now
Example output:
2026-04-22 10:15:30.123456
2026-04-22
14:30:00
2026-04-29 10:15:30.123456
Use this as a quick example of the most common datetime module objects.
What the datetime module is for
You use the datetime module when your program needs to work with:
- Dates
- Times
- Dates and times together
- Time differences
- Converting between date objects and strings
Common uses include:
- Getting the current date and time
- Creating a birthday or deadline
- Adding 7 days to a date
- Showing a date in a readable format
- Reading a date from user input
Main objects in the datetime module
The datetime module contains several useful classes.
date
A date stores:
- Year
- Month
- Day
Example:
from datetime import date
d = date(2026, 4, 22)
print(d)
Output:
2026-04-22
Use date when you only care about the calendar date.
time
A time stores:
- Hour
- Minute
- Second
- Smaller units like microseconds if needed
Example:
from datetime import time
t = time(14, 30)
print(t)
Output:
14:30:00
Use time when you only need a clock time.
datetime
A datetime stores both:
- Date
- Time
Example:
from datetime import datetime
dt = datetime(2026, 4, 22, 14, 30)
print(dt)
Output:
2026-04-22 14:30:00
Use datetime when you need the full date and time together.
If you want the current date and time, see datetime.now() explained.
timedelta
A timedelta stores a length of time.
You can use it to add or subtract:
- Days
- Hours
- Minutes
- Seconds
Example:
from datetime import datetime, timedelta
now = datetime.now()
later = now + timedelta(days=3)
print(now)
print(later)
Use timedelta when you want to move forward or backward in time.
timezone
A timezone object helps represent time zone information.
For beginners, it is enough to know that time zones exist and can matter when you work with users in different places.
This page does not go deep into time zone handling.
When to use each object
Here is a simple guide:
- Use
datewhen you only need a calendar date - Use
timewhen you only need a clock time - Use
datetimewhen you need both date and time - Use
timedeltato add or subtract days, hours, or seconds - Leave deeper time zone handling for dedicated pages
Examples:
from datetime import date, time, datetime, timedelta
birthday = date(2026, 9, 10)
alarm_time = time(7, 0)
appointment = datetime(2026, 9, 10, 15, 45)
next_week = appointment + timedelta(days=7)
print(birthday)
print(alarm_time)
print(appointment)
print(next_week)
Common beginner tasks
These are some of the most common things beginners do with datetime.
Get the current date with date.today()
from datetime import date
today = date.today()
print(today)
Get the current date and time with datetime.now()
from datetime import datetime
now = datetime.now()
print(now)
For a focused explanation, see datetime.now() explained.
Format a datetime as text with strftime()
strftime() turns a date or datetime object into a string.
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M")
print(formatted)
Possible output:
2026-04-22 10:15
If you want to learn format codes step by step, see datetime.strftime() explained.
Parse text into a datetime with strptime()
strptime() turns a string into a datetime object.
from datetime import datetime
text = "2026-04-22 14:30"
dt = datetime.strptime(text, "%Y-%m-%d %H:%M")
print(dt)
Output:
2026-04-22 14:30:00
For more examples, see datetime.strptime() explained.
Add days with timedelta(days=...)
from datetime import date, timedelta
today = date.today()
future_date = today + timedelta(days=10)
print(today)
print(future_date)
How importing works
This is one of the biggest beginner confusion points.
import datetime
This imports the whole module.
import datetime
print(datetime.date.today())
print(datetime.datetime.now())
Here, you must use the module name first:
datetime.datedatetime.datetimedatetime.timedelta
from datetime import datetime
This imports only the datetime class.
from datetime import datetime
print(datetime.now())
Now datetime refers to the class, not the module.
Why this confuses beginners
The module is named datetime, and one class inside it is also named datetime.
That means these two lines do different things:
import datetime
from datetime import datetime
If you mix them up, you may get an error such as an AttributeError. If that happens, see how to fix "module has no attribute".
A quick way to remember it:
import datetime→ usedatetime.datetime.now()from datetime import datetime→ usedatetime.now()
What this page does not cover in depth
This overview does not go deep into:
- Detailed formatting codes
- Advanced time zone handling
- Date arithmetic edge cases
- Third-party libraries like
pandasordateutil
This page is meant to help you understand the main pieces of the module first.
Common mistakes
These are some common beginner problems when using datetime.
Confusing the datetime module with the datetime class
This is very common:
import datetime
print(datetime.now())
This causes a problem because now() belongs to the datetime class, not directly to the module.
Correct versions:
import datetime
print(datetime.datetime.now())
or:
from datetime import datetime
print(datetime.now())
Using strptime() with the wrong format string
If the text and the format do not match, Python raises an error.
from datetime import datetime
text = "22/04/2026"
dt = datetime.strptime(text, "%Y-%m-%d")
This fails because the string uses / and day-first order, but the format expects year-month-day.
Correct version:
from datetime import datetime
text = "22/04/2026"
dt = datetime.strptime(text, "%d/%m/%Y")
print(dt)
Trying to add a string directly to a datetime object
This does not work:
from datetime import datetime
now = datetime.now()
# print(now + "7 days")
You must use timedelta instead:
from datetime import datetime, timedelta
now = datetime.now()
result = now + timedelta(days=7)
print(result)
Expecting date objects to have time values
A date only stores:
- Year
- Month
- Day
It does not store hours or minutes.
from datetime import date
today = date.today()
print(today)
If you need the time too, use datetime.
Forgetting that months and days must be valid numbers
This will fail:
from datetime import date
# bad_date = date(2026, 13, 40)
Month must be from 1 to 12, and day must be valid for that month.
If you see value conversion problems in other code, you may also need to check your input first. A related beginner error is invalid literal for int() with base 10.
FAQ
What is the difference between date and datetime in Python?
date stores only year, month, and day. datetime stores both the date and the time.
Why does Python have both datetime the module and datetime the class?
The module contains several classes. One of those classes is also named datetime.
How do I get the current date and time?
Use datetime.now() after importing the datetime class from the datetime module.
from datetime import datetime
print(datetime.now())
How do I turn a datetime into a string?
Use strftime() to format it as text.
from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d"))
How do I turn a string into a datetime?
Use strptime() with a matching format pattern.
from datetime import datetime
text = "2026-04-22"
dt = datetime.strptime(text, "%Y-%m-%d")
print(dt)
See also
- datetime.now() explained
- datetime.strptime() explained
- datetime.strftime() explained
- How to fix "module has no attribute"
If you are learning Python step by step, the best next move is to pick one exact task: getting the current date, parsing a date string, or formatting a datetime for output.