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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。