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


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

Python Calendar.monthdays2calendar() 方法

Calendar.monthdays2calendar() 方法是內置的方法CalendarcalendarPython 中的模塊。它使用此類的實例並返回給定月份中的周列表作為完整周。周,這裏給出的是七個元組的列表,其中每個元組由當天的天數和工作日數組成。由於周被寫為完整的周,因此月外的天數表示為 0。

模塊:

    import calendar

類:

    from calendar import Calendar

用法:

    monthdays2calendar(year, month)

參數:

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

返回值:

這個方法的返回類型是<class 'list'>,它返回給定月份中的周列表,其中每個元組代表該日期的日期和日期。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2018
month = 11

# Here first value is the day of the month
# and second value is the weekday number 
# where Monday is 0 till Sunday which is 6
print("Days outside of the month are 0")
print("Weekwise calendar of November 2018 with first weekday as Monday")
print(cal.monthdays2calendar(year, month))
print()
# Note tuples always start from firstweekday value
# Full weeks are listed.

# set the firstweekday to 1
cal = calendar.Calendar(firstweekday = 5)
year = 1994
month = 4

print("Weekwise calendar of November 2011 with first weekday as Saturday")
print(cal.monthdays2calendar(year, month))
print()

輸出

Days outside of the month are 0
Weekwise calendar of November 2018 with first weekday as Monday
[[(0, 0), (0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6)], [(5, 0), (6, 1), (7, 2), (8, 3), (9, 4), (10, 5), (11, 6)], [(12, 0), (13, 1), (14, 2), (15, 3), (16, 4), (17, 5), (18, 6)], [(19, 0), (20, 1), (21, 2), (22, 3), (23, 4), (24, 5), (25, 6)], [(26, 0), (27, 1), (28, 2), (29, 3), (30, 4), (0, 5), (0, 6)]]

Weekwise calendar of November 2011 with first weekday as Saturday
[[(0, 5), (0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (1, 4)], [(2, 5), (3, 6), (4, 0), (5, 1), (6, 2), (7, 3), (8, 4)], [(9, 5), (10, 6), (11, 0), (12, 1), (13, 2), (14, 3), (15, 4)], [(16, 5), (17, 6), (18, 0), (19, 1), (20, 2), (21, 3), (22, 4)], [(23, 5), (24, 6), (25, 0), (26, 1), (27, 2), (28, 3), (29, 4)], [(30, 5), (0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]]


相關用法


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