Python Calendar itermonthdays2() Method with Example

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

Python Calendar.itermonthdays2() Method

The Calendar.itermonthdays2() method is an inbuilt method of the Calendar class of the calendar module, it returns an iterator for the given month in the given year. Days returned are tuples where the first value is the day of the month and the second value is the weekday number, i.e., the day of the week on that date. The function returns full weeks, ie, each week in the month will have 7 tuples and the dates outside of the month will have 0 value in the first value of the tuple.

For example, (0, 3) means it is outside of that month and it is Thursday on this day.

Module

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

import calendar

Class

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

from calendar import Calendar

Syntax

The following is the syntax of itermonthdays2() method:

itermonthdays2(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 'generator'>, it returns an iterator for the given month of the given year. The day returned is a tuple representing the date and weekday on that given day. Also, remember that full weeks are printed.

Example of Calendar.itermonthdays2() Method in Python

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2009
month = 2

print("Iterating over the weeks of February 2009 where each tuple is date and weekday on that day")
print("Days outside the month are written as 0")
print("Iteration starts from weekday/second value as 0, ie, Monday")
for i in cal.itermonthdays2(year, month):
    print(i)
# first value is the day of the month; 
# days outside of the month is 0
# second value is weekday number where 
# Monday is 0 till Sunday which is 6
print()
print()

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

print("Iterating over the weeks of January 2020 where each tuple is date and weekday on that day")
print("Iteration starts from weekday/second value as 4, ie, Friday")
for i in cal.itermonthdays2(year, month):
    print(i)
print()
print()

Output

Iterating over the weeks of February 2009 where each tuple is date and weekday on that day
Days outside the month are written as 0
Iteration starts from weekday/second value as 0, ie, Monday
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(1, 6)
(2, 0)
(3, 1)
(4, 2)
(5, 3)
(6, 4)
(7, 5)
(8, 6)
(9, 0)
(10, 1)
(11, 2)
(12, 3)
(13, 4)
(14, 5)
(15, 6)
(16, 0)
(17, 1)
(18, 2)
(19, 3)
(20, 4)
(21, 5)
(22, 6)
(23, 0)
(24, 1)
(25, 2)
(26, 3)
(27, 4)
(28, 5)
(0, 6)


Iterating over the weeks of January 2020 where each tuple is date and weekday on that day
Iteration starts from weekday/second value as 4, ie, Friday
(0, 4)
(0, 5)
(0, 6)
(0, 0)
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
(6, 0)
(7, 1)
(8, 2)
(9, 3)
(10, 4)
(11, 5)
(12, 6)
(13, 0)
(14, 1)
(15, 2)
(16, 3)
(17, 4)
(18, 5)
(19, 6)
(20, 0)
(21, 1)
(22, 2)
(23, 3)
(24, 4)
(25, 5)
(26, 6)
(27, 0)
(28, 1)
(29, 2)
(30, 3)
(31, 4)
(0, 5)
(0, 6)
(0, 0)
(0, 1)
(0, 2)
(0, 3)



Comments and Discussions!

Load comments ↻





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