Sub topic 1
The building blocks: date, time, and datetime
Python's datetime module has three core types for calendar values: date, time, and datetime. Every other tool in the module, including time zones, builds on them. This chapter shows how to create each type, read its parts, and combine or modify them.
Prerequisites
You should be comfortable with basic Python syntax: functions, imports, and handling exceptions with try and except. You should be running Python 3.9 or later. You don't need any time zone knowledge yet. This chapter uses only values without a time zone.
Three types, three jobs
dateholds a year, month, and day.timeholds an hour, minute, second, and microsecond.datetimeholds all of those together as one point on a calendar.
All three are immutable. Once you create an object, you can't change it. Operations that look like edits return a new object.
Creating a date and a time
Pass the parts to the constructor, from the largest unit to the smallest.
from datetime import date, time
d = date(2026, 11, 26)
print(d.year, d.month, d.day)
print(d.weekday(), d.isoweekday())
print(d.isoformat())
t = time(11, 7, 30, 250000)
print(t)
print(t.hour, t.minute, t.second, t.microsecond)This prints:
2026 11 26
3 4
2026-11-26
11:07:30.250000
11 7 30 250000The two weekday methods use different numbering. weekday() counts Monday as 0, so Thursday is 3. isoweekday() counts Monday as 1, so Thursday is 4. Pick one and stay consistent.
Any time field you leave out defaults to 0. That means time(11, 7) is 11:07:00.
Invalid values raise ValueError
The constructors check their arguments against the calendar.
from datetime import date
try:
date(2026, 2, 30)
except ValueError as e:
print(e)This prints day is out of range for month. Python never rolls an impossible date forward into March for you.
Building a datetime
You can give datetime all the fields directly, or build one from a date and a time with datetime.combine. The first form is datetime(2026, 11, 26, 11, 7). Here is the second.
from datetime import date, time, datetime, timedelta
d = date(2026, 11, 26)
t = time(11, 7)
dt = datetime.combine(d, t)
print(dt)
print(dt + timedelta(days=10, hours=2))This prints:
2026-11-26 11:07:00
2026-12-06 13:07:00The timedelta in the last line is a duration. The next chapter covers it in detail. For now, notice that adding one to a datetime returns a new datetime.
Going the other way, dt.date() and dt.time() pull the parts back out.
Replacing fields
Because the objects are immutable, use replace to get a modified copy. It accepts any field as a keyword argument.
from datetime import datetime
dt = datetime(2026, 11, 26, 11, 7, 30, 250000)
midnight = dt.replace(hour=0, minute=0, second=0, microsecond=0)
print(midnight)
print(dt)
print(dt.date(), dt.time())This prints:
2026-11-26 00:00:00
2026-11-26 11:07:30.250000
2026-11-26 11:07:30.250000The original dt is unchanged. Setting the time fields to zero, as above, is a common way to get the start of a day.
Comparing values
Objects of the same type compare in the order you expect. Earlier values are smaller.
from datetime import date
print(date(2026, 11, 26) < date(2026, 12, 1))
print(date(2026, 11, 26) == date(2026, 11, 26))Both lines print True. A datetime is also greater than any earlier datetime, and you can sort lists of them directly.
Be careful with mixed types. A date and a datetime are different types. Comparing them for order raises a TypeError, and == returns False. Convert with dt.date() first if you want to compare days.
Current date and time
date.today() returns today's date. datetime.now() returns the current moment. Both use the computer's local clock and attach no time zone information. That is fine for quick scripts, but it causes bugs in real programs. The chapters on naive and aware datetimes explain why and show the fix.
Summary of the types
| Type | Holds | Example |
|---|---|---|
| `date` | year, month, day | `date(2026, 11, 26)` |
| `time` | hour, minute, second, microsecond | `time(11, 7)` |
| `datetime` | all seven fields | `datetime(2026, 11, 26, 11, 7)` |
Exercise
What does print(datetime.combine(date(2026, 12, 25), time(8, 30)).replace(hour=20)) output, and why does the result differ from the combined value?
Show answer
It prints 2026-12-25 20:30:00. combine first produces 2026-12-25 08:30:00. replace(hour=20) then returns a new datetime with only the hour changed. The original object is immutable, so replace never edits it.
Exercise
Why does date(2026, 4, 31) fail, and which exception does it raise?
Show answer
April has 30 days, so the day is outside the valid range for that month. The constructor raises ValueError with the message "day is out of range for month".