Python provides two related but different ways to work with minimum and maximum dates.
date.min and date.max give the minimum and maximum dates supported by datetime.date.min() and max() find the earliest and latest dates from a collection.date.min, date.max, datetime.min and datetime.max are attributes. Do not add parentheses after them.The date class provides the earliest and latest dates that it can represent.
from datetime import date
print(date.min)
print(date.max)
Output
0001-01-01
9999-12-31
These are class attributes, so use:
date.min
date.max
and not:
# Incorrect
date.min()
date.max()
The datetime class includes both date and time, so its minimum and maximum values include time components.
from datetime import datetime
print(datetime.min)
print(datetime.max)
Output
0001-01-01 00:00:00
9999-12-31 23:59:59.999999
Creating a separate datetime object is not necessary just to access these limits. The values belong to the datetime class itself.
| Attribute | Value |
|---|---|
date.min | 0001-01-01 |
date.max | 9999-12-31 |
datetime.min | 0001-01-01 00:00:00 |
datetime.max | 9999-12-31 23:59:59.999999 |
Python's built-in min() and max() functions can compare date objects directly.
from datetime import date
dates=[
date(2022, 5, 4),
date(2021, 1, 1),
date(2023, 12, 12)
]
print(
'Earliest Date:',
min(dates)
)
print(
'Latest Date:',
max(dates)
)
Output
Earliest Date: 2021-01-01
Latest Date: 2023-12-12
Python compares dates chronologically, so the earliest date becomes the minimum and the latest date becomes the maximum.
date.min means the minimum date supported by Python. min(dates) means the earliest date in your own collection. These are different operations.Date strings can sometimes be compared directly, but this is only reliable when their text format sorts in the same order as the dates.
ISO-style YYYY-MM-DD strings satisfy this condition.
my_dates=[
'2021-03-23',
'2019-04-15',
'2021-07-26',
'2019-12-22'
]
print(
min(my_dates)
)
print(
max(my_dates)
)
Output
2019-04-15
2021-07-26
This works because year comes first, followed by zero-padded month and day.
Consider dates stored in DD-MM-YYYY format:
dates=[
'25-01-2023',
'03-12-2022'
]
Comparing these strings directly compares the text from left to right. It does not understand that the second string represents an earlier year.
For reliable date comparisons, convert the strings to date objects first.
from datetime import datetime
dates=[
'25-01-2023',
'03-12-2022'
]
date_objects=[
datetime.strptime(
value,
'%d-%m-%Y'
).date()
for value in dates
]
print(
min(date_objects)
)
print(
max(date_objects)
)
Output
2022-12-03
2023-01-25
The same min() and max() functions work with datetime objects.
from datetime import datetime
event_dates=[
datetime(2022, 5, 20),
datetime(2023, 2, 18),
datetime(2021, 8, 30)
]
print(
'First Event:',
min(event_dates)
)
print(
'Last Event:',
max(event_dates)
)
Output
First Event: 2021-08-30 00:00:00
Last Event: 2023-02-18 00:00:00
Subtract the earliest date from the latest date to find the total span covered by a dataset.
from datetime import date
dates=[
date(2022, 5, 4),
date(2021, 1, 1),
date(2023, 12, 12)
]
earliest=min(dates)
latest=max(dates)
date_range=latest - earliest
print(date_range)
print(date_range.days)
Output
1075 days, 0:00:00
1075
Subtracting two date values returns a timedelta object. Its .days attribute gives the number of days between the two dates.
Use strftime() to change the display format.
first_event=min(event_dates)
formatted=first_event.strftime(
'%d-%m-%Y'
)
print(
'First Event:',
formatted
)
Output
First Event: 30-08-2021
Python strftime() Date Formats
The earliest and latest dates in a collection can also define an application's permitted date range.
from datetime import datetime
user_input='2022-07-10'
input_date=datetime.strptime(
user_input,
'%Y-%m-%d'
).date()
available_dates=[
datetime(2022, 5, 20).date(),
datetime(2023, 2, 18).date(),
datetime(2021, 8, 30).date()
]
min_date=min(available_dates)
max_date=max(available_dates)
if min_date <= input_date <= max_date:
print(
'Date is inside the allowed range.'
)
else:
print(
'Date is outside the allowed range.'
)
Output
Date is inside the allowed range.
This checks whether the date falls between the earliest and latest boundaries. It does not check whether the exact date exists in the original list.
If exact membership is required, use:
if input_date in available_dates:
print(
'Date is available.'
)
datetime.min and datetime.max are timezone-naive values by default.
If a UTC-aware boundary is required, Python's built-in timezone.utc can be used without installing another library.
from datetime import datetime, timezone
dt_min=datetime.min.replace(
tzinfo=timezone.utc
)
dt_max=datetime.max.replace(
tzinfo=timezone.utc
)
print(dt_min)
print(dt_max)
Output
0001-01-01 00:00:00+00:00
9999-12-31 23:59:59.999999+00:00
min() and max() return the extreme values. Use sorted() when all events should be arranged chronologically.
from datetime import datetime
events=[
(
'Event A',
datetime(2023, 5, 17)
),
(
'Event B',
datetime(2021, 7, 23)
),
(
'Event C',
datetime(2022, 9, 12)
)
]
events_sorted=sorted(
events,
key=lambda item: item[1]
)
for event in events_sorted:
print(
event[0],
'on',
event[1].strftime(
'%Y-%m-%d'
)
)
Output
Event B on 2021-07-23
Event C on 2022-09-12
Event A on 2023-05-17
The key argument can also be used with min() or max().
earliest_event=min(
events,
key=lambda item: item[1]
)
latest_event=max(
events,
key=lambda item: item[1]
)
print(earliest_event)
print(latest_event)
This returns the complete event tuple rather than only the date.
Incorrect:
date.min()
date.max()
Correct:
date.min
date.max
date.min is Python's minimum representable date.
date.min
min(dates) finds the earliest date in your collection.
min(dates)
Direct string comparison is safe only when the string format is chronologically sortable, such as zero-padded YYYY-MM-DD. Convert other formats to date objects before comparing them.
This raises ValueError:
dates=[]
min(dates)
Check the list first:
if dates:
earliest=min(dates)
Alternatively, the built-in functions support a default value when using a single iterable:
earliest=min(
dates,
default=None
)
latest=max(
dates,
default=None
)
Keep the values being compared consistent. Use all date objects or all datetime objects rather than relying on mixed-type comparisons.
date.min is the earliest date supported by Python's date class: 0001-01-01.date.max is the latest supported date: 9999-12-31.datetime.min and datetime.max also include time components.min() to find the earliest date in a collection.max() to find the latest date.YYYY-MM-DD strings can be compared lexicographically when they use consistent zero-padded formatting.min(dates) from max(dates) to calculate the total date range.strftime() to format the result.key argument can find earliest or latest records when a date is stored inside a larger object such as a tuple.timezone.utc when UTC-aware minimum or maximum datetime boundaries are required.default=None with min() or max() when an iterable may be empty.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.