On this page
calendar.monthrange
Prev: calendar.monthcalendar | Up: calendar | Next: calendar.prcal
Rel: calendar.calendar, calendar.Calendar, calendar.Day, calendar.firstweekday
Ref: docs.python.org — calendar.monthrange
Tags: #public #python3 #stdlib #calendar
Import
import calendar
from calendar import monthrange
Signature & Overview
calendar.monthrange(year, month)
Return weekday of first day of month (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.
Usage
import calendar
first_wday, num_days = calendar.monthrange(2026, 2)
assert num_days == 28 and first_wday in range(7)
Gotchas
- Best way to find the number of days in any
(year, month): Handles leap years automatically.
REPL Inspect
from calendar import monthrange
help(monthrange)
Linked from
Related notes
- tuts/python3/stdlib_module/calendar/monthcalendar Linked here; links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/prcal Linked here; links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/calendar Linked here; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/Calendar_class Linked here; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/Day Linked here; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/firstweekday Linked here; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/calendar/HTMLCalendar shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/IllegalMonthError shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/IllegalWeekdayError shared tags: #calendar, #stdlib