Python monthdayscalendar(): Get Complete Weeks of a Month

calendar.Calendar.monthdayscalendar(year, month) returns a list of weeks. Each week contains seven integer day numbers. Days outside the requested month appear as 0, giving every week the same length.

import calendar
my_cal= calendar.Calendar()
for x in my_cal.monthdayscalendar(2020,7):
    print(x)

Output:

[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]

The July 2020 calendar starts on Wednesday. The first week has two 0 placeholders before day 1, and the last week ends with two placeholders after day 31.

Practice Python monthdayscalendar()

Run the July 2020 example, change the first weekday, count real days, and try exercises with automatic checks.

Open Python monthdayscalendar practice notebook in Google Colab View notebook on GitHub

No installation needed in Colab; the notebook uses Python's standard library.

On this page — Show page sections (jump to a topic)— Hide page sections

Syntax and parameters

Call the method on a calendar.Calendar instance: Calendar(firstweekday=0).monthdayscalendar(year, month). The arguments are integers: year is the calendar year and month ranges from 1 to 12.

ParameterDescriptionExample
yearYear of the requested month2020
monthMonth number from 1 (January) to 12 (December)7
firstweekdayOptional setting on the Calendar instance, not a parameter of monthdayscalendar(); Monday = 0, Sunday = 6calendar.Calendar(firstweekday=3)

The return type is list[list[int]]: the outer list contains weeks, and each inner list contains exactly seven day numbers. It does not return tuples or datetime.date objects. See Python lists.

import calendar
weeks = calendar.Calendar().monthdayscalendar(2020, 7)
print(type(weeks).__name__)
print(type(weeks[0]).__name__)
print(weeks[0])

Output:

list
list
[0, 0, 1, 2, 3, 4, 5]

The first week is another list, not a tuple or date object.

Start the week on Thursday

The original alternative example sets firstweekday=3, meaning Thursday. The same day numbers appear, but the columns and the number of displayed week rows can change. For more, see setting the first weekday.

import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.monthdayscalendar(2020,7):
    print(x)

Output:

[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]

Count weeks and days per week

Because the result is a nested list, use len() to count weeks, and len(weeks[0]) to confirm the number of day slots in a week. This retains the original lesson example:

import calendar
my_cal= calendar.Calendar(firstweekday=0)
y=my_cal.monthdayscalendar(2020,7)
print("Number of weeks",len(y))
print("Number of days in week",len(y[0]))

Output:

Number of weeks 5
Number of days in week 7

For a month, the returned calendar usually occupies four, five, or six complete week rows, depending on the number of days and first weekday. Every row always has seven entries.

Loop through every day slot

You can use nested for loops to read the day numbers from each week. This reproduces the original demonstration, with the repeated output shortened for readability:

import calendar
my_cal= calendar.Calendar(firstweekday=0)
for x in my_cal.monthdayscalendar(2020,7):
    for y in x:
        print("Day :",y)

Output excerpt:

Day : 0
Day : 0
Day : 1
Day : 2
Day : 3
Day : 4
Day : 5
Day : 6
... (middle lines omitted) ...
Day : 30
Day : 31
Day : 0
Day : 0

A printed Day : 0 is a placeholder, not a date in the previous or next month.

Ignore the zero placeholders

When you need the actual dates in July, skip values equal to zero. This is useful before calculating counts, sums, or task assignments.

import calendar
weeks = calendar.Calendar().monthdayscalendar(2020, 7)
days = [day for week in weeks for day in week if day != 0]
print(days[:7])
print(days[-5:])
print("Days in month:", len(days))

Output:

[1, 2, 3, 4, 5, 6, 7]
[27, 28, 29, 30, 31]
Days in month: 31

Find the first and last day numbers

Flatten the weekly lists first, then remove the placeholders. That makes the first and last real day numbers easy to identify.

import calendar
weeks = calendar.Calendar().monthdayscalendar(2020, 7)
real_days = [day for week in weeks for day in week if day]
print("First:", real_days[0])
print("Last:", real_days[-1])

Output:

First: 1
Last: 31

Number the calendar weeks

Use enumerate() to label rows as Week 1, Week 2, and so on. These are rows within this month view, not ISO week numbers.

import calendar
weeks = calendar.Calendar().monthdayscalendar(2020, 7)
for week_number, week in enumerate(weeks, start=1):
    print("Week", week_number, ":", week)

Output:

Week 1 : [0, 0, 1, 2, 3, 4, 5]
Week 2 : [6, 7, 8, 9, 10, 11, 12]
Week 3 : [13, 14, 15, 16, 17, 18, 19]
Week 4 : [20, 21, 22, 23, 24, 25, 26]
Week 5 : [27, 28, 29, 30, 31, 0, 0]

Add weekday names above the grid

The weekday numbers are always Monday 0 through Sunday 6. iterweekdays() lets you generate a heading in the same order as the selected first weekday. Read the iterweekdays() tutorial for more.

import calendar
my_cal = calendar.Calendar(firstweekday=3)
headings = [calendar.day_abbr[d] for d in my_cal.iterweekdays()]
print(" ".join(headings))
for week in my_cal.monthdayscalendar(2020, 7):
    print(" ".join(f"{day:>3}" if day else "  -" for day in week))

Output:

Thu Fri Sat Sun Mon Tue Wed
  -   -   -   -   -   -   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   -   -   -   -   -

This formatting makes it easier to read the output as a calendar. The result from monthdayscalendar() itself contains numbers, not text.

Count weekdays and weekend days

Weekday position in a week follows the calendar object’s start day. To calculate Monday–Friday correctly when you change the starting day, pair each position with iterweekdays() rather than assuming index 0 always means Monday.

import calendar
my_cal = calendar.Calendar(firstweekday=6)
weekday_numbers = list(my_cal.iterweekdays())
weekdays = weekends = 0
for week in my_cal.monthdayscalendar(2020, 7):
    for weekday, day in zip(weekday_numbers, week):
        if day == 0:
            continue
        if weekday < 5:
            weekdays += 1
        else:
            weekends += 1
print("Monday-Friday:", weekdays)
print("Saturday-Sunday:", weekends)

Output:

Monday-Friday: 23
Saturday-Sunday: 8

Find the week containing a date

Find a day number inside its week row. This is useful for highlighting a selected week in a booking calendar or weekly study planner.

import calendar
weeks = calendar.Calendar().monthdayscalendar(2020, 7)
target_day = 15
for index, week in enumerate(weeks, start=1):
    if target_day in week:
        print("Day", target_day, "is in calendar row", index)
        print(week)
        break

Output:

Day 15 is in calendar row 3
[13, 14, 15, 16, 17, 18, 19]

Change the calendar to a Sunday-first layout

Setting firstweekday=6 starts each row on Sunday. The real dates remain the same; only their positions in the grid move.

import calendar
my_cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
for week in my_cal.monthdayscalendar(2020, 7):
    print(week)

Output:

[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]

Calendar.monthdayscalendar() versus calendar.monthcalendar()

The module-level calendar.monthcalendar(year, month) returns the same day-number grid for the module’s configured first weekday. Using a Calendar instance is convenient when different parts of an application need different starting days.

import calendar
weeks_a = calendar.Calendar(firstweekday=0).monthdayscalendar(2020, 7)
weeks_b = calendar.monthcalendar(2020, 7)
print(weeks_a == weeks_b)

Output:

True

This comparison assumes the module-level default first weekday has not been changed earlier by calendar.setfirstweekday().

Compare related calendar methods

Choose the representation that matches your task: day numbers, weekday tuples, or actual date objects. The related itermonthdays(), itermonthdays2(), and itermonthdays3() tutorials explain alternative iterator forms.

import calendar
my_cal = calendar.Calendar()
print("Day numbers:", my_cal.monthdayscalendar(2020, 7)[0])
print("(day, weekday):", my_cal.monthdays2calendar(2020, 7)[0][:3])
print("date objects:", my_cal.monthdatescalendar(2020, 7)[0][:3])

Output:

Day numbers: [0, 0, 1, 2, 3, 4, 5]
(day, weekday): [(0, 0), (0, 1), (1, 2)]
date objects: [datetime.date(2020, 6, 29), datetime.date(2020, 6, 30), datetime.date(2020, 7, 1)]

With monthdayscalendar(), out-of-month positions are 0; itermonthdates() and monthdatescalendar() instead return the actual adjacent-month dates.

Leap-year example: February 2024

Leap years change the number of real days, but each calendar row remains seven entries long.

import calendar
weeks = calendar.Calendar().monthdayscalendar(2024, 2)
real_days = [d for week in weeks for d in week if d]
print("Weeks:", len(weeks))
print("Days:", len(real_days))
print("Last day:", real_days[-1])

Output:

Weeks: 5
Days: 29
Last day: 29

Use the rows in a simple weekly task planner

A list of week rows is a natural structure for a planning display. For example, collect real July dates into weekly batches and leave empty slots out of the work plan.

import calendar
weeks = calendar.Calendar().monthdayscalendar(2020, 7)
for i, week in enumerate(weeks, start=1):
    scheduled_days = [d for d in week if d != 0]
    print("Schedule week", i, ":", scheduled_days)

Output:

Schedule week 1 : [1, 2, 3, 4, 5]
Schedule week 2 : [6, 7, 8, 9, 10, 11, 12]
Schedule week 3 : [13, 14, 15, 16, 17, 18, 19]
Schedule week 4 : [20, 21, 22, 23, 24, 25, 26]
Schedule week 5 : [27, 28, 29, 30, 31]

Common mistakes and troubleshooting

  • Counting zeros as days: ignore 0 before calculating the number of dates or scheduling tasks.
  • Confusing lists and tuples: the method returns a list of lists, not a list of tuples. See tuples for the difference.
  • Assuming every month uses five rows: a month view can require four to six complete week rows.
  • Assuming column index 0 always means Monday: the starting weekday can be changed, so use iterweekdays() to interpret columns.
  • Passing an invalid month: month numbers must be between 1 and 12.
  • Expecting dates outside the month: this method uses 0 placeholders; use monthdatescalendar() for full date objects.
import calendar
try:
    weeks = calendar.Calendar().monthdayscalendar(2020, 13)
except calendar.IllegalMonthError:
    print("Choose a month from 1 to 12")

Output:

Choose a month from 1 to 12

Try these practice exercises

Use the Colab practice notebook to change the examples and check your answers automatically. Start with these questions:

  • Print the weekly grid for your birth month and count its rows.
  • Change firstweekday to Sunday and verify that every row still has seven entries.
  • Count how many real days appear in February 2024 (ignore zeros).
  • Find which week row contains day 20 of a month.
  • Display weekday names over the calendar grid.

Frequently asked questions

What does monthdayscalendar() return?

A Python list of weeks. Each week is another list containing seven integer day numbers.

Why are some elements zero?

A zero is used for a slot belonging to the previous or next month, so every displayed week has seven positions.

Does changing firstweekday change weekday numbers?

No. Monday remains 0 and Sunday remains 6. Only the order of the columns changes.

Can the output contain four or six weeks?

Yes. The number of rows depends on the month and which weekday starts each calendar week.

How do I get actual dates instead of day numbers?

Use monthdatescalendar() or itermonthdates(). Those return datetime.date values for the adjacent-month days as well.

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer