python 示例
Python Calendar.itermonthdays()方法 (Python Calendar.itermonthdays() Method)
Calendar.itermonthdays() method is an inbuilt method of the Calendar class of calendar module in Python. It uses an instance of this class and returns an iterator for the given month in the given year. Days returned are the days of the month. Here, each week of the month as full 7 days, so if a day lies outside of the specified month, it is given value 0.
Calendar.itermonthdays()方法是Python中Calendar模块的Calendar类的内置方法。 它使用此类的实例,并返回给定年份中给定月份的迭代器。 返回的天数是一个月中的天数。 在这里,该月的每个星期为整整7天,因此,如果某天不在指定的月份内,则其值将为0。
Module:
模块:
import calendar
Class:
类:
from calendar import Calendar
Syntax:
句法:
itermonthdays(year, month)
Parameter(s):
参数:
year: It is a required parameter, which specifies the year of the calendar.
year :这是必填参数,用于指定日历的年份。
month: It is a required parameter, which specifies the month of the calendar.
month :这是必填参数,用于指定日历的月份。
Return value:
返回值:
The return type of this method is <class 'generator'>, it returns an iterator for the given month which gives you the days of the month starting from the first weekday.
此方法的返回类型为<class'generator'> ,它返回给定月份的迭代器,该迭代器为您提供从第一个工作日开始的每月的几天。
Example:
例:
# Python program to illustrate the
# use of itermonthdays() method
# import class
import calendar
# Creating Calendar Instance
cal = calendar.Calendar()
year = 2009
month = 2
# Month starts with Sunday and weekday is set to 0
# So monday till saturday will be 0 and they represent
# previous month days
print("Iterating over February 2009 where each value gives the day of the month")
for i in cal.itermonthdays(year, month):
print(i)
print()
# Setting firstweekday value to 2
cal = calendar.Calendar(firstweekday = 2)
year = 2016
month = 12
# Month starts with Thursday and firstweekday is set to 2
# wednesday will be 0 and that represents
# previous month day
print("Iterating over December 2016 where each value gives the day of the month")
for i in cal.itermonthdays(year, month):
print(i)
Output
输出量
Iterating over February 2009 where each value gives the day of the month
0
0
0
0
0
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
Iterating over December 2016 where each value gives the day of the month
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
0
0
翻译自: https://www.includehelp.com/python/calendar-calendar-itermonthdays-method-with-example.aspx
python 示例