python 示例_Python日历类| 带示例的itermonthdays()方法

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 示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值