Python Calendar monthdatescalendar() Method with Example

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

Python Calendar.monthdatescalendar() Method

The Calendar.monthdatescalendar() method is an inbuilt method of the Calendar class of the calendar module, it returns a list of the weeks in the given month of the given year. Weeks are listed as full weeks, ie, each week will have 7 values even if the value of that day lies outside that month. Each week's list consists of seven datetime.date objects.

Module

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

import calendar

Class

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

from calendar import Calendar

Syntax

The following is the syntax of monthdatescalendar() method:

monthdatescalendar(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 consisting of 7 date objects.

Example of Calendar.monthdatescalendar() Method in Python

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2019
month = 12
# datetime.date format: (year, month, day)
print(cal.monthdatescalendar(year, month))
print()
# Note iterator always start from firstweekday value

# set the firstweekday to 1
cal = calendar.Calendar(firstweekday = 1)
year = 1994
month = 9
print(cal.monthdatescalendar(year, month))
print()

Output

[[datetime.date(2019, 11, 25), datetime.date(2019, 11, 26), datetime.date(2019, 11, 27), datetime.date(2019, 11, 28), datetime.date(2019, 11, 29), datetime.date(2019, 11, 30), datetime.date(2019, 12, 1)], [datetime.date(2019, 12, 2), datetime.date(2019, 12, 3), datetime.date(2019, 12, 4), datetime.date(2019, 12, 5), datetime.date(2019, 12, 6), datetime.date(2019, 12, 7), datetime.date(2019, 12, 8)], [datetime.date(2019, 12, 9), datetime.date(2019, 12, 10), datetime.date(2019, 12, 11), datetime.date(2019, 12, 12), datetime.date(2019, 12, 13), datetime.date(2019, 12, 14), datetime.date(2019, 12, 15)], [datetime.date(2019, 12, 16), datetime.date(2019, 12, 17), datetime.date(2019, 12, 18), datetime.date(2019, 12, 19), datetime.date(2019, 12, 20), datetime.date(2019, 12, 21), datetime.date(2019, 12, 22)], [datetime.date(2019, 12, 23), datetime.date(2019, 12, 24), datetime.date(2019, 12, 25), datetime.date(2019, 12, 26), datetime.date(2019, 12, 27), datetime.date(2019, 12, 28), datetime.date(2019, 12, 29)], [datetime.date(2019, 12, 30), datetime.date(2019, 12, 31), datetime.date(2020, 1, 1), datetime.date(2020, 1, 2), datetime.date(2020, 1, 3), datetime.date(2020, 1, 4), datetime.date(2020, 1, 5)]]

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



Comments and Discussions!

Load comments ↻





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