Python Calendar monthdays2calendar() Method with Example

Python Calendar.monthdays2calendar() Method: In this tutorial, we will learn about the monthdays2calendar() method of Calendar class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python Calendar.monthdays2calendar() Method

The Calendar.monthdays2calendar() method is an inbuilt method of the Calendar class of calendar module, it returns a list of the weeks in the given month as full weeks. Weeks, given here are lists of seven tuples, where each tuple consists of the day number and the weekday number of that day. Since weeks are written as full weeks, days outside the month are represented as 0.

Module

The following module is required to use monthdays2calendar() method:

import calendar

Class

The following class is required to use monthdays2calendar() method:

from calendar import Calendar

Syntax

The following is the syntax of monthdays2calendar() method:

monthdays2calendar(year, month)

Parameter(s)

The following are the parameter(s):

  • year: It is a required parameter, which specifies the year of the calendar.
  • month: It is a required parameter, which specifies the month of the calendar.

Return Value

The return type of this method is <class 'list'>, it returns a list of the weeks in the given month, where each tuple represents the day and date on that date.

Example of Calendar.monthdays2calendar() Method in Python

# Python program to illustrate the 
# use of monthdays2calendar() method

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2018
month = 11

# Here first value is the day of the month
# and second value is the weekday number 
# where Monday is 0 till Sunday which is 6
print("Days outside of the month are 0")
print("Weekwise calendar of November 2018 with first weekday as Monday")
print(cal.monthdays2calendar(year, month))
print()
# Note tuples always start from firstweekday value
# Full weeks are listed.

# set the firstweekday to 1
cal = calendar.Calendar(firstweekday = 5)
year = 1994
month = 4

print("Weekwise calendar of November 2011 with first weekday as Saturday")
print(cal.monthdays2calendar(year, month))
print()

Output

Days outside of the month are 0
Weekwise calendar of November 2018 with first weekday as Monday
[[(0, 0), (0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6)], [(5, 0), (6, 1), (7, 2), (8, 3), (9, 4), (10, 5), (11, 6)], [(12, 0), (13, 1), (14, 2), (15, 3), (16, 4), (17, 5), (18, 6)], [(19, 0), (20, 1), (21, 2), (22, 3), (23, 4), (24, 5), (25, 6)], [(26, 0), (27, 1), (28, 2), (29, 3), (30, 4), (0, 5), (0, 6)]]

Weekwise calendar of November 2011 with first weekday as Saturday
[[(0, 5), (0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (1, 4)], [(2, 5), (3, 6), (4, 0), (5, 1), (6, 2), (7, 3), (8, 4)], [(9, 5), (10, 6), (11, 0), (12, 1), (13, 2), (14, 3), (15, 4)], [(16, 5), (17, 6), (18, 0), (19, 1), (20, 2), (21, 3), (22, 4)], [(23, 5), (24, 6), (25, 0), (26, 1), (27, 2), (28, 3), (29, 4)], [(30, 5), (0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]]



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.