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


Python Calendar itermonthdays3()用法及代码示例


Python Calendar.itermonthdays3() 方法

Calendar.itermonthdays3() 方法是内置的方法CalendarcalendarPython 中的模块。它使用此类的实例并返回给定年份中给定月份的迭代器。该月由整周组成,即,即使该值在该月之外,每周也有整整 7 个值。一周中的条目由一个元组给出,该元组由年、月和月中的某一天组成。

模块:

    import calendar

类:

    from calendar import Calendar

用法:

    itermonthdays3(year, month)

参数:

  • year:它是一个必需参数,它指定日历的年份。
  • month:它是一个必需参数,它指定日历的月份。

返回值:

这个方法的返回类型是<class 'generator'>,它返回一个月份的迭代器,其中每个值都是一个元组,由当天的年、月和日期组成。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2019
month = 12

print("Iterating over the weeks of December 2019 where each tuple is date, month and year")
for i in cal.itermonthdays3(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 1
cal = calendar.Calendar(firstweekday = 1)
year = 1994
month = 9

print("Iterating over the weeks of September 1994 where each tuple is date, month and year and iterator starts with firstweekday as Tuesday")
for i in cal.itermonthdays3(year, month):
    print(i)
print()
print()

输出

Iterating over the weeks of December 2019 where each tuple is date, month and year
(2019, 11, 25)
(2019, 11, 26)
(2019, 11, 27)
(2019, 11, 28)
(2019, 11, 29)
(2019, 11, 30)
(2019, 12, 1)
(2019, 12, 2)
(2019, 12, 3)
(2019, 12, 4)
(2019, 12, 5)
(2019, 12, 6)
(2019, 12, 7)
(2019, 12, 8)
(2019, 12, 9)
(2019, 12, 10)
(2019, 12, 11)
(2019, 12, 12)
(2019, 12, 13)
(2019, 12, 14)
(2019, 12, 15)
(2019, 12, 16)
(2019, 12, 17)
(2019, 12, 18)
(2019, 12, 19)
(2019, 12, 20)
(2019, 12, 21)
(2019, 12, 22)
(2019, 12, 23)
(2019, 12, 24)
(2019, 12, 25)
(2019, 12, 26)
(2019, 12, 27)
(2019, 12, 28)
(2019, 12, 29)
(2019, 12, 30)
(2019, 12, 31)
(2020, 1, 1)
(2020, 1, 2)
(2020, 1, 3)
(2020, 1, 4)
(2020, 1, 5)


Iterating over the weeks of September 1994 where each tuple is date, month and year and iterator starts with firstweekday as Tuesday
(1994, 8, 30)
(1994, 8, 31)
(1994, 9, 1)
(1994, 9, 2)
(1994, 9, 3)
(1994, 9, 4)
(1994, 9, 5)
(1994, 9, 6)
(1994, 9, 7)
(1994, 9, 8)
(1994, 9, 9)
(1994, 9, 10)
(1994, 9, 11)
(1994, 9, 12)
(1994, 9, 13)
(1994, 9, 14)
(1994, 9, 15)
(1994, 9, 16)
(1994, 9, 17)
(1994, 9, 18)
(1994, 9, 19)
(1994, 9, 20)
(1994, 9, 21)
(1994, 9, 22)
(1994, 9, 23)
(1994, 9, 24)
(1994, 9, 25)
(1994, 9, 26)
(1994, 9, 27)
(1994, 9, 28)
(1994, 9, 29)
(1994, 9, 30)
(1994, 10, 1)
(1994, 10, 2)
(1994, 10, 3)


相关用法


注:本文由纯净天空筛选整理自 Python Calendar Class | itermonthdays3() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。