当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。