all Python 3calendarfunction
calendar.monthrange()
dates and leap years
Returns a pair: the weekday of the first day of the month and the number of days in the month.
Syntax
first_weekday, days = calendar.monthrange(year, month)
Despite the name, it is not the first and last day: the first item is a weekday (0 is Monday), the second is the length of the month. The last day of a month is monthrange(y, m)[1]. Leap years are handled: February 2024 gives 29. Since 3.12 the weekday is a calendar.Day member, but it is still an integer.
Examples
import calendar
first, days = calendar.monthrange(2024, 2)
print(first, days) # → 3 29 — четверг, 29 дней
print(calendar.monthrange(2023, 2)[1]) # → 28
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy