Calendar Module:
The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.
monthdatescalendar() Method:
The monthdatescalendar() method returns a list of full weeks for a given month of the year. Weeks are lists of seven datetime. date objects.
Syntax:
monthdatescalendar(year, month)
Note:
datetime.date: A date object represents a date (year, month, and day) in an idealised calendar, which is the existing Gregorian calendar expanded in both directions indefinitely. January 1 of year 1 is referred to as day number 1, January 2 of year 1 is referred to as day number 2, and so on. This corresponds to the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold’s book Calendrical Calculations, where it serves as the base for all computations.
Parameter Values:
year:Â This is required. It is a number. The year for which the calendar should be created.
month:Â This is required. It is a number. The month for which the calendar should be created.
Return Value: This function returns a list of weeks in the month.
Program for calendar monthdatescalendar() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Example1: Using For Loop
Approach:
- Import calendar module using the import keyword.
- Give the year as static input and store it in a variable.
- Give the month as static input and store it in another variable.
- Call the Calendar() function and store it in another variable.
- Apply monthdatescalendar() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
- Iterate in the above result using the for loop.
- Inside the loop, print the iterator value.
- The Exit of the Program.
Below is the implementation:
# Import calendar module using the import keyword. import calendar # Give the year as static input and store it in a variable. gvn_yr = 2020 # Give the month as static input and store it in another variable. gvn_mont = 4 # Call the Calendar() function and store it in another variable. calendr = calendar.Calendar() # Apply monthdatescalendar() method to the above calendar by passing the given year, # month as the arguments and store it in another variable. rslt = calendr.monthdatescalendar(gvn_yr, gvn_mont) # Iterate in the above result using the for loop. for itr in rslt: # Inside the loop, print the iterator value. print(itr)
Output:
[datetime.date(2020, 3, 30), datetime.date(2020, 3, 31), datetime.date(2020, 4, 1), datetime.date(2020, 4, 2), datetime.date(2020, 4, 3), datetime.date(2020, 4, 4), datetime.date(2020, 4, 5)] [datetime.date(2020, 4, 6), datetime.date(2020, 4, 7), datetime.date(2020, 4, 8), datetime.date(2020, 4, 9), datetime.date(2020, 4, 10), datetime.date(2020, 4, 11), datetime.date(2020, 4, 12)] [datetime.date(2020, 4, 13), datetime.date(2020, 4, 14), datetime.date(2020, 4, 15), datetime.date(2020, 4, 16), datetime.date(2020, 4, 17), datetime.date(2020, 4, 18), datetime.date(2020, 4, 19)] [datetime.date(2020, 4, 20), datetime.date(2020, 4, 21), datetime.date(2020, 4, 22), datetime.date(2020, 4, 23), datetime.date(2020, 4, 24), datetime.date(2020, 4, 25), datetime.date(2020, 4, 26)] [datetime.date(2020, 4, 27), datetime.date(2020, 4, 28), datetime.date(2020, 4, 29), datetime.date(2020, 4, 30), datetime.date(2020, 5, 1), datetime.date(2020, 5, 2), datetime.date(2020, 5, 3)]
Example2:
Approach:
- Import calendar module using the import keyword.
- Give the year as static input and store it in a variable.
- Give the month as static input and store it in another variable.
- Call the Calendar() function and store it in another variable.
- Apply monthdatescalendar() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import calendar module using the import keyword. import calendar # Give the year as static input and store it in a variable. gvn_yr = 2012 # Give the month as static input and store it in another variable. gvn_mont = 6 # Call the Calendar() function and store it in another variable. calendr = calendar.Calendar() # Apply monthdatescalendar() method to the above calendar by passing the given year, # month as the arguments and store it in another variable. rslt = calendr.monthdatescalendar(gvn_yr, gvn_mont) # Print the above result. print(rslt)
Output:
[[datetime.date(2012, 5, 28), datetime.date(2012, 5, 29), datetime.date(2012, 5, 30), datetime.date(2012, 5, 31), datetime.date(2012, 6, 1), datetime.date(2012, 6, 2), datetime.date(2012, 6, 3)], [datetime.date(2012, 6, 4), datetime.date(2012, 6, 5), datetime.date(2012, 6, 6), datetime.date(2012, 6, 7), datetime.date(2012, 6, 8), datetime.date(2012, 6, 9), datetime.date(2012, 6, 10)], [datetime.date(2012, 6, 11), datetime.date(2012, 6, 12), datetime.date(2012, 6, 13), datetime.date(2012, 6, 14), datetime.date(2012, 6, 15), datetime.date(2012, 6, 16), datetime.date(2012, 6, 17)], [datetime.date(2012, 6, 18), datetime.date(2012, 6, 19), datetime.date(2012, 6, 20), datetime.date(2012, 6, 21), datetime.date(2012, 6, 22), datetime.date(2012, 6, 23), datetime.date(2012, 6, 24)], [datetime.date(2012, 6, 25), datetime.date(2012, 6, 26), datetime.date(2012, 6, 27), datetime.date(2012, 6, 28), datetime.date(2012, 6, 29), datetime.date(2012, 6, 30), datetime.date(2012, 7, 1)]]
Method #2: Using Built-in Functions (User Input)
Example1: Using For Loop
Approach:
- Import calendar module using the import keyword.
- Give the year as user input using the int(input()) function and store it in a variable.
- Give the month as user input using the int(input()) function and store it in another variable.
- Call the Calendar() function and store it in another variable.
- Apply monthdatescalendar() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
- Iterate in the above result using the for loop.
- Inside the loop, print the iterator value.
- The Exit of the Program.
Below is the implementation:
# Import calendar module using the import keyword. import calendar # Give the year as user input using the int(input()) function and store it in a variable. gvn_yr = int(input("Enter some random year = ")) # Give the month as user input using the int(input()) function and store it in another variable. gvn_mont = int(input("Enter some random month = ")) # Call the Calendar() function and store it in another variable. calendr = calendar.Calendar() # Apply monthdatescalendar() method to the above calendar by passing the given year, # month as the arguments and store it in another variable. rslt = calendr.monthdatescalendar(gvn_yr, gvn_mont) # Iterate in the above result using the for loop. for itr in rslt: # Inside the loop, print the iterator value. print(itr)
Output:
Enter some random year = 2017 Enter some random month = 3 [datetime.date(2017, 2, 27), datetime.date(2017, 2, 28), datetime.date(2017, 3, 1), datetime.date(2017, 3, 2), datetime.date(2017, 3, 3), datetime.date(2017, 3, 4), datetime.date(2017, 3, 5)] [datetime.date(2017, 3, 6), datetime.date(2017, 3, 7), datetime.date(2017, 3, 8), datetime.date(2017, 3, 9), datetime.date(2017, 3, 10), datetime.date(2017, 3, 11), datetime.date(2017, 3, 12)] [datetime.date(2017, 3, 13), datetime.date(2017, 3, 14), datetime.date(2017, 3, 15), datetime.date(2017, 3, 16), datetime.date(2017, 3, 17), datetime.date(2017, 3, 18), datetime.date(2017, 3, 19)] [datetime.date(2017, 3, 20), datetime.date(2017, 3, 21), datetime.date(2017, 3, 22), datetime.date(2017, 3, 23), datetime.date(2017, 3, 24), datetime.date(2017, 3, 25), datetime.date(2017, 3, 26)] [datetime.date(2017, 3, 27), datetime.date(2017, 3, 28), datetime.date(2017, 3, 29), datetime.date(2017, 3, 30), datetime.date(2017, 3, 31), datetime.date(2017, 4, 1), datetime.date(2017, 4, 2)]