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


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


Python Calendar.monthdatescalendar() 方法

Calendar.monthdatescalendar() 方法是内置的方法CalendarcalendarPython 中的模块。它使用此类的实例并返回给定年份的给定月份中的周列表。周被列为完整周,即,即使该天的值在该月之外,每周也将有 7 个值。每个周列表由七个 datetime.date 对象组成。

模块:

    import calendar

类:

    from calendar import Calendar

用法:

    monthdatescalendar(year, month)

参数:

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

返回值:

这个方法的返回类型是<class 'list'>,它返回给定月份中的周列表,由 7 个日期对象组成。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2019
month = 12
# datetime.date format:(year, month, day)
print(cal.monthdatescalendar(year, month))
print()
# Note iterator always start from firstweekday value

# set the firstweekday to 1
cal = calendar.Calendar(firstweekday = 1)
year = 1994
month = 9
print(cal.monthdatescalendar(year, month))
print()

输出

[[datetime.date(2019, 11, 25), datetime.date(2019, 11, 26), datetime.date(2019, 11, 27), datetime.date(2019, 11, 28), datetime.date(2019, 11, 29), datetime.date(2019, 11, 30), datetime.date(2019, 12, 1)], [datetime.date(2019, 12, 2), datetime.date(2019, 12, 3), datetime.date(2019, 12, 4), datetime.date(2019, 12, 5), datetime.date(2019, 12, 6), datetime.date(2019, 12, 7), datetime.date(2019, 12, 8)], [datetime.date(2019, 12, 9), datetime.date(2019, 12, 10), datetime.date(2019, 12, 11), datetime.date(2019, 12, 12), datetime.date(2019, 12, 13), datetime.date(2019, 12, 14), datetime.date(2019, 12, 15)], [datetime.date(2019, 12, 16), datetime.date(2019, 12, 17), datetime.date(2019, 12, 18), datetime.date(2019, 12, 19), datetime.date(2019, 12, 20), datetime.date(2019, 12, 21), datetime.date(2019, 12, 22)], [datetime.date(2019, 12, 23), datetime.date(2019, 12, 24), datetime.date(2019, 12, 25), datetime.date(2019, 12, 26), datetime.date(2019, 12, 27), datetime.date(2019, 12, 28), datetime.date(2019, 12, 29)], [datetime.date(2019, 12, 30), datetime.date(2019, 12, 31), datetime.date(2020, 1, 1), datetime.date(2020, 1, 2), datetime.date(2020, 1, 3), datetime.date(2020, 1, 4), datetime.date(2020, 1, 5)]]

[[datetime.date(1994, 8, 30), datetime.date(1994, 8, 31), datetime.date(1994, 9, 1), datetime.date(1994, 9, 2), datetime.date(1994, 9, 3), datetime.date(1994, 9, 4), datetime.date(1994, 9, 5)], [datetime.date(1994, 9, 6), datetime.date(1994, 9, 7), datetime.date(1994, 9, 8), datetime.date(1994, 9, 9), datetime.date(1994, 9, 10), datetime.date(1994, 9, 11), datetime.date(1994, 9, 12)], [datetime.date(1994, 9, 13), datetime.date(1994, 9, 14), datetime.date(1994, 9, 15), datetime.date(1994, 9, 16), datetime.date(1994, 9, 17), datetime.date(1994, 9, 18), datetime.date(1994, 9, 19)], [datetime.date(1994, 9, 20), datetime.date(1994, 9, 21), datetime.date(1994, 9, 22), datetime.date(1994, 9, 23), datetime.date(1994, 9, 24), datetime.date(1994, 9, 25), datetime.date(1994, 9, 26)], [datetime.date(1994, 9, 27), datetime.date(1994, 9, 28), datetime.date(1994, 9, 29), datetime.date(1994, 9, 30), datetime.date(1994, 10, 1), datetime.date(1994, 10, 2), datetime.date(1994, 10, 3)]]


相关用法


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