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


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


Python Calendar.itermonthdays2() 方法

Calendar.itermonthdays2() 方法是内置的方法CalendarcalendarPython 中的模块。它使用此类的实例并返回给定年份中给定月份的迭代器。返回的天数是元组,其中第一个值是一个月中的第一个值,第二个值是工作日数,即该日期的星期几。该函数返回完整的周,即,一个月中的每个星期将有 7 个元组,而月外的日期在元组的第一个值中将有 0 个值。

例如,(0, 3) 这意味着它在该月之外并且今天是星期四。

模块:

    import calendar

类:

    from calendar import Calendar

用法:

    itermonthdays2(year, month)

参数:

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

返回值:

这个方法的返回类型是<class 'generator'>,它返回给定年份的给定月份的迭代器。返回的日期是一个元组,表示给定日期的日期和工作日。另外,请记住打印整周。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2009
month = 2

print("Iterating over the weeks of February 2009 where each tuple is date and weekday on that day")
print("Days outside the month are written as 0")
print("Iteration starts from weekday/second value as 0, ie, Monday")
for i in cal.itermonthdays2(year, month):
    print(i)
# first value is the day of the month; 
# days outside of the month is 0
# second value is weekday number where 
# Monday is 0 till Sunday which is 6
print()
print()

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

print("Iterating over the weeks of January 2020 where each tuple is date and weekday on that day")
print("Iteration starts from weekday/second value as 4, ie, Friday")
for i in cal.itermonthdays2(year, month):
    print(i)
print()
print()

输出

Iterating over the weeks of February 2009 where each tuple is date and weekday on that day
Days outside the month are written as 0
Iteration starts from weekday/second value as 0, ie, Monday
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(1, 6)
(2, 0)
(3, 1)
(4, 2)
(5, 3)
(6, 4)
(7, 5)
(8, 6)
(9, 0)
(10, 1)
(11, 2)
(12, 3)
(13, 4)
(14, 5)
(15, 6)
(16, 0)
(17, 1)
(18, 2)
(19, 3)
(20, 4)
(21, 5)
(22, 6)
(23, 0)
(24, 1)
(25, 2)
(26, 3)
(27, 4)
(28, 5)
(0, 6)


Iterating over the weeks of January 2020 where each tuple is date and weekday on that day
Iteration starts from weekday/second value as 4, ie, Friday
(0, 4)
(0, 5)
(0, 6)
(0, 0)
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
(6, 0)
(7, 1)
(8, 2)
(9, 3)
(10, 4)
(11, 5)
(12, 6)
(13, 0)
(14, 1)
(15, 2)
(16, 3)
(17, 4)
(18, 5)
(19, 6)
(20, 0)
(21, 1)
(22, 2)
(23, 3)
(24, 4)
(25, 5)
(26, 6)
(27, 0)
(28, 1)
(29, 2)
(30, 3)
(31, 4)
(0, 5)
(0, 6)
(0, 0)
(0, 1)
(0, 2)
(0, 3)


相关用法


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