📜  Python日历模块 | itermonthdates() 方法

📅  最后修改于: 2022-05-13 01:54:19.513000             🧑  作者: Mango

Python日历模块 | itermonthdates() 方法

日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。
itermonthdates()方法返回一年中月份 (1-12) 的迭代器。此迭代器将返回该月的所有天数以及该月开始之前或月末之后的所有天数,这些天数需要获得完整的一周。

Syntax: itermonthdates(year, month)

Parameter: 
year: year of the calendar
month: month of the calendar

Returns: an iterator for the month.

代码#1:

Python3
# Python program to demonstrate working
# of itermonthdates() method
 
# importing calendar module
from calendar import Calendar
 
obj = calendar.Calendar()
 
# iterating with itermonthdates
for day in obj.itermonthdates(2018, 9):
    print(day)


Python3
# Python program to demonstrate working
# of itermonthdates() method
 
# importing calendar module
import calendar
 
# use with firstweekday = 5
obj = calendar.Calendar(firstweekday = 5)
 
# iterating with itermonthdates
for day in obj.itermonthdates(2018, 4):
    print(day)


输出:

2018-08-27
2018-08-28
2018-08-29
2018-08-30
2018-08-31
.
.
.
2018-09-26
2018-09-27
2018-09-28
2018-09-29
2018-09-30


代码#2:

Python3

# Python program to demonstrate working
# of itermonthdates() method
 
# importing calendar module
import calendar
 
# use with firstweekday = 5
obj = calendar.Calendar(firstweekday = 5)
 
# iterating with itermonthdates
for day in obj.itermonthdates(2018, 4):
    print(day)

输出:

2018-08-30
2018-08-31
2018-09-01
2018-09-02
2018-09-03
.
.
2018-09-30
2018-10-01
2018-10-02
2018-10-03
2018-09-01
2018-09-02
2018-09-03
.
.
2018-09-28
.
.
0018-03-31
0018-04-01
0018-04-02
.
.
2018-04-28
2018-04-29
2018-04-30
2018-05-01
2018-05-02