Python calendar itermonthdays method with example

Published by admin on

Python calendar itermonthdays example

Introduction :

itermonthdays is defined in python calendar module. This method has four variants :

itermonthdays(year, month)
itermonthdays2(year, month)
itermonthdays3(year, month)
itermonthdays4(year, month)

All of these methods return one iterator for the given month and year. It returns all the days in the month. If the month starts or ends in the middle of a week, it returns 0 for the other days for that week those are not in included in the month. itermonthdays2, itermonthdays3 and itermonthdays4 returns one tuple for each day, not a number.

 

Example of itermonthdays :

Let’s consider the below example :

import calendar

cal = calendar.Calendar()

for day in cal.itermonthdays(2020, 5):
    print(day)

It will print the below output :

0
0
0
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

It printed four zeros and 1 to 31. Because, 1st May of 2020 is Friday. If we check the week from Monday as 1, it is on fifth position. So, it places four zeros at start. Again, 31st May is Sunday, so it doesn’t print any zeroes at end.

 

Example of itermonthdays2 :

import calendar

cal = calendar.Calendar()

for day in cal.itermonthdays2(2020, 6):
    print(day)

 

Output :

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

First June of 2020 is Monday and 30th is Tuesday.

 

Example of itermonthdays3 :

import calendar

cal = calendar.Calendar()

for day in cal.itermonthdays3(2020, 8):
    print(day)

It prints the month detail in full :

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

 

Example of itermonthdays4 :

import calendar

cal = calendar.Calendar()

for day in cal.itermonthdays4(2021, 12):
    print(day)

This method prints the year, month and day :

(2021, 11, 29, 0)
(2021, 11, 30, 1)
(2021, 12, 1, 2)
(2021, 12, 2, 3)
(2021, 12, 3, 4)
(2021, 12, 4, 5)
(2021, 12, 5, 6)
(2021, 12, 6, 0)
(2021, 12, 7, 1)
(2021, 12, 8, 2)
(2021, 12, 9, 3)
(2021, 12, 10, 4)
(2021, 12, 11, 5)
(2021, 12, 12, 6)
(2021, 12, 13, 0)
(2021, 12, 14, 1)
(2021, 12, 15, 2)
(2021, 12, 16, 3)
(2021, 12, 17, 4)
(2021, 12, 18, 5)
(2021, 12, 19, 6)
(2021, 12, 20, 0)
(2021, 12, 21, 1)
(2021, 12, 22, 2)
(2021, 12, 23, 3)
(2021, 12, 24, 4)
(2021, 12, 25, 5)
(2021, 12, 26, 6)
(2021, 12, 27, 0)
(2021, 12, 28, 1)
(2021, 12, 29, 2)
(2021, 12, 30, 3)
(2021, 12, 31, 4)
(2022, 1, 1, 5)
(2022, 1, 2, 6)

itermonthdays3 and itermonthdays4 were added in python 3.7.

 

Similar tutorials :

Categories: python

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *