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


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


Python Calendar.itermonthdates() 方法

Calendar.itermonthdates() 方法是 Python 中日历模块的 Calendar 类的内置方法。它使用此类的实例并返回给定年份中给定月份(1-12)的迭代器。迭代器将返回该月的所有天数(作为 datetime.date 对象)以及该月开始之前或月底之后的所有天数,这些天数需要获得完整的一周。

模块:

    import calendar

类:

    from calendar import Calendar

用法:

    itermonthdates(year, month)

参数:

  • year:代表日历年
  • month:代表日历的月份

返回值:

该函数返回给定年份的给定月份的迭代器。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2020
month = 2
# Complete weeks will be printed
for i in cal.itermonthdates(year, month):
    print(i)
print()

# Setting firstweekday value to 3
cal = calendar.Calendar(firstweekday = 3)
year = 2016
month = 2
for i in cal.itermonthdates(year, month):
    print(i)

输出

2020-01-27
2020-01-28
2020-01-29
2020-01-30
2020-01-31
2020-02-01
2020-02-02
2020-02-03
2020-02-04
2020-02-05
2020-02-06
2020-02-07
2020-02-08
2020-02-09
2020-02-10
2020-02-11
2020-02-12
2020-02-13
2020-02-14
2020-02-15
2020-02-16
2020-02-17
2020-02-18
2020-02-19
2020-02-20
2020-02-21
2020-02-22
2020-02-23
2020-02-24
2020-02-25
2020-02-26
2020-02-27
2020-02-28
2020-02-29
2020-03-01

2016-01-28
2016-01-29
2016-01-30
2016-01-31
2016-02-01
2016-02-02
2016-02-03
2016-02-04
2016-02-05
2016-02-06
2016-02-07
2016-02-08
2016-02-09
2016-02-10
2016-02-11
2016-02-12
2016-02-13
2016-02-14
2016-02-15
2016-02-16
2016-02-17
2016-02-18
2016-02-19
2016-02-20
2016-02-21
2016-02-22
2016-02-23
2016-02-24
2016-02-25
2016-02-26
2016-02-27
2016-02-28
2016-02-29
2016-03-01
2016-03-02

参考:calendar.Calendar.itermonthdates



相关用法


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