當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python Calendar itermonthdays()用法及代碼示例


Python Calendar.itermonthdays() 方法

Calendar.itermonthdays() 方法是內置的方法CalendarcalendarPython 中的模塊。它使用此類的實例並返回給定年份中給定月份的迭代器。返回的天數是該月的天數。在這裏,一個月中的每個星期都是完整的 7 天,所以如果一天不在指定的月份,它的值為 0。

模塊:

    import calendar

類:

    from calendar import Calendar

用法:

    itermonthdays(year, month)

參數:

  • year:它是一個必需參數,它指定日曆的年份。
  • month:它是一個必需參數,它指定日曆的月份。

返回值:

這個方法的返回類型是<class 'generator'>,它返回給定月份的迭代器,它為您提供從第一個工作日開始的月份中的天數。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2009
month = 2
# Month starts with Sunday and weekday is set to 0
# So monday till saturday will be 0 and they represent 
# previous month days
print("Iterating over February 2009 where each value gives the day of the month")
for i in cal.itermonthdays(year, month):
    print(i)
print()


# Setting firstweekday value to 2
cal = calendar.Calendar(firstweekday = 2)
year = 2016
month = 12
# Month starts with Thursday and firstweekday is set to 2
# wednesday will be 0 and that represents 
# previous month day
print("Iterating over December 2016 where each value gives the day of the month")
for i in cal.itermonthdays(year, month):
    print(i)

輸出

Iterating over February 2009 where each value gives the day of the month
0
0
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
0

Iterating over December 2016 where each value gives the day of the month
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
0
0
0


相關用法


注:本文由純淨天空篩選整理自 Python Calendar Class | itermonthdays() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。