Currently to get the last day of the month, the most straightforward method is:
from calendar import monthrange
from datetime import date
mydate = date.today()
mydate.replace(day=monthrange(mydate.year, mydate.month)[1]) # date(2025, 9, 30)
monthrange() returns a tuple (dayofweek, numberofdaysinmonth), therefore taking the second element we have the number of last day of month.
Could we extend allowed input for day argument in the replace method for negative integers to be able to type:
from datetime import date
mydate = date.today()
mydate.replace(day=-1) # date(2025, 9, 30)
to get the last day of a given month?
By analogy to list indexing, this should be perfectly readable (and maybe even expected?) for a Python programmer.
Sample implementation (patch on CPython’s main branch):
Index: Lib/_pydatetime.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py
--- a/Lib/_pydatetime.py (revision cf9ef73121cd2b96dd514e09b3ad253d841d91dd)
+++ b/Lib/_pydatetime.py (date 1758057891128)
@@ -1178,6 +1178,8 @@
month = self._month
if day is None:
day = self._day
+ elif day < 0:
+ day = _days_in_month(year, month) + 1 + day
return type(self)(year, month, day)
__replace__ = replace
@@ -2090,6 +2092,8 @@
month = self.month
if day is None:
day = self.day
+ elif day < 0:
+ day = _days_in_month(year, month) + 1 + day
if hour is None:
hour = self.hour
if minute is None:
Alternatively we could extend the behaviour of date/datetime constructors in this matter.
Update: I’ve edited the title to express the discussion direction (LAST sentinel).
Update: I’ve edited the title again (end_of_month).