Get the Number of Days and Weeks in a Month in Python

Modified: | Tags: Python, Date and time

This article explains how to get the number of days and weeks in a month with the calendar module in Python.

In this article, the number of calendar rows is considered the number of weeks in the month. This number changes based on the selected start day of the week. Specific examples are provided later.

For methods on creating text and HTML calendars using the calendar module, refer to the following article.

Get the number of days in a month: calendar.monthrange()

The calendar.monthrange() function returns a tuple containing the first day of the month (Monday is 0, Sunday is 6) and the number of days in the month. Provide the year as the first argument and the month as the second.

To get only the number of days, specify index [1].

import calendar

print(calendar.monthrange(2023, 1))
# (6, 31)

print(calendar.monthrange(2023, 1)[1])
# 31

Leap years are also accounted for in this calculation.

print(calendar.monthrange(2023, 2)[1])
# 28

print(calendar.monthrange(2024, 2)[1])
# 29

Note that the calendar module also defines calendar._monthlen(), which directly returns the number of days in a month.

However, its underscore prefix indicates it is meant for internal use. It is not officially documented and is not included in __all__, making calendar.monthrange() the preferable choice.

print(calendar._monthlen(2023, 1))
# 31

print(calendar.__all__)
# ['IllegalMonthError', 'IllegalWeekdayError', 'setfirstweekday', 'firstweekday', 'isleap', 'leapdays', 'weekday', 'monthrange', 'monthcalendar', 'prmonth', 'month', 'prcal', 'calendar', 'timegm', 'month_name', 'month_abbr', 'day_name', 'day_abbr', 'Calendar', 'TextCalendar', 'HTMLCalendar', 'LocaleTextCalendar', 'LocaleHTMLCalendar', 'weekheader', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY']

print('monthrange' in calendar.__all__)
# True

print('_monthlen' in calendar.__all__)
# False

Get the number of weeks in a month

The calendar.monthcalendar() function creates the calendar as a two-dimensional list (list of lists). For enhanced readability, this example uses the pprint module.

import calendar
import pprint

print(calendar.month(2023, 1))
#     January 2023
# Mo Tu We Th Fr Sa Su
#                    1
#  2  3  4  5  6  7  8
#  9 10 11 12 13 14 15
# 16 17 18 19 20 21 22
# 23 24 25 26 27 28 29
# 30 31
# 

pprint.pprint(calendar.monthcalendar(2023, 1))
# [[0, 0, 0, 0, 0, 0, 1],
#  [2, 3, 4, 5, 6, 7, 8],
#  [9, 10, 11, 12, 13, 14, 15],
#  [16, 17, 18, 19, 20, 21, 22],
#  [23, 24, 25, 26, 27, 28, 29],
#  [30, 31, 0, 0, 0, 0, 0]]

You can get the number of weeks on the calendar using the built-in len() function.

print(len(calendar.monthcalendar(2023, 1)))
# 6

Note on the first day of the week

By default, calendar.monthcalendar() sets Monday as the week's start. To change this, use calendar.setfirstweekday().

calendar.setfirstweekday(calendar.SUNDAY)

print(calendar.month(2023, 1))
#     January 2023
# Su Mo Tu We Th Fr Sa
#  1  2  3  4  5  6  7
#  8  9 10 11 12 13 14
# 15 16 17 18 19 20 21
# 22 23 24 25 26 27 28
# 29 30 31
# 

pprint.pprint(calendar.monthcalendar(2023, 1))
# [[1, 2, 3, 4, 5, 6, 7],
#  [8, 9, 10, 11, 12, 13, 14],
#  [15, 16, 17, 18, 19, 20, 21],
#  [22, 23, 24, 25, 26, 27, 28],
#  [29, 30, 31, 0, 0, 0, 0]]

print(len(calendar.monthcalendar(2023, 1)))
# 5

Be aware that the number of weeks in a month may vary depending on the selected start day of the week.

Since calendar.SUNDAY and similar constants are integers (calendar.MONDAY is 0, calendar.SUNDAY is 6), you can also directly use integer values with calendar.setfirstweekday().

The current setting can be checked using calendar.firstweekday().

print(calendar.MONDAY)
# 0

print(calendar.SUNDAY)
# 6

calendar.setfirstweekday(0)

print(calendar.firstweekday())
# 0

Related Categories

Related Articles