Python日历模块 | itermonthdays2() 方法
日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。
itermonthdays2()
方法用于获取一年中月份的迭代器,类似于itermonthdates()
。返回的天数将只是月份数字。对于指定月份以外的天数,天数为 0。
Syntax: itermonthdays2(year, month)
Parameter:
year: year of the calendar
month: month of the calendar
Returns: an iterator for the month.
代码 #1: itermonthdays2() 方法的工作示例
# Python program to demonstrate working
# of itermonthdates() method
# importing calendar module
import calendar
obj = calendar.Calendar()
# iterating with itermonthdays2
for day in obj.itermonthdays2(2018, 9):
print(day)
输出:
The starting day number in calendar is : 0
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 5)
(2, 6)
(3, 0)
.
.
(29, 5)
(30, 6)
代码 #2: itermonthdays2() 方法的工作示例,firstweekday=2
# Python program to demonstrate working
# of itermonthdates() method
# importing calendar module
import calendar
obj = calendar.Calendar(firstweekday = 2)
# iterating with parameter itermonthdays2
for day in obj.itermonthdays2(2018, 9):
print(day)
输出:
(0, 2)
(0, 3)
(0, 4)
(1, 5)
(2, 6)
(3, 0)
(4, 1)
.
.
(27, 3)
(28, 4)
(29, 5)
(30, 6)
(0, 0)
(0, 1)