ADVERTISEMENT
ADVERTISEMENT

Python Calendar Class | monthdatescalendar() Method with Example

Python Calendar.monthdatescalendar() Method: Here, we are going to learn about the monthdatescalendar() method of Calendar class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 10, 2020

Python Calendar.monthdatescalendar() Method

Calendar.monthdatescalendar() method is an inbuilt method of the Calendar class of calendar module in Python. It uses an instance of this class and 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 list consists of seven datetime.date objects.

Module:

    import calendar

Class:

    from calendar import Calendar

Syntax:

    monthdatescalendar(year, month)

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:

# 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)]]
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.