📜  Python日历模块:monthdatescalendar() 方法

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

Python日历模块:monthdatescalendar() 方法

日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。

Python中的monthdatescalendar()方法用于获取一年中月份中的周列表作为整周。

Syntax: monthdatescalendar(year, month)

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

Returns: a list of the weeks in the month.

代码#1:

Python3
# Python program to demonstrate working
# of monthdatescalendar() method
 
# importing calendar module
import calendar
 
obj = calendar.Calendar()
 
year = 2018
month = 9
 
# printing with monthdatescalendar
print(obj.monthdatescalendar(year, month))


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


输出:

请注意,输出中的周是七个 datetime.date 对象的列表。代码 #2:迭代周列表

Python3

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

输出: