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


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