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


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


Python Calendar.monthdayscalendar() 方法

Calendar.monthdayscalendar() 方法是內置的方法CalendarcalendarPython 中的模塊。它使用此類的實例並返回給定年份給定月份中的周列表作為完整周。周是七個數字的列表。如果一周中某一天的值為 0,則表示它不屬於該月。

模塊:

    import calendar

類:

    from calendar import Calendar

用法:

    monthdayscalendar(year, month)

參數:

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

返回值:

這個方法的返回類型是<class 'list'>,它將給定月份中的周列表作為完整周返回,即,每周由 7 個值組成。這裏有 0 值的天意味著他們不在那個月。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
# default firstweekday =0
year = 2011
month = 11

print("Days outside of the month are 0")
print("Weekwise calendar of November 2011 with first weekday as Monday")
print(cal.monthdayscalendar(year, month))
print()
# always full weeks are listed.

# set the firstweekday to 3
cal = calendar.Calendar(firstweekday = 3)
year = 1998
month = 8

print("Weekwise calendar of August 1998 with first weekday as Thursday")
print(cal.monthdayscalendar(year, month))
print()

輸出

Days outside of the month are 0
Weekwise calendar of November 2011 with first weekday as Monday
[
    [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, 0, 0, 0, 0]
]

Weekwise calendar of August 1998 with first weekday as Thursday
[
    [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, 0, 0]
]


相關用法


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