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

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

Python日历模块:yeardatescalendar() 方法

日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。
Python中的yeardatescalendar()方法用于获取一年中月份的周列表作为整周。周列表中的条目是天数。本月以外的天数为零。

Syntax: yeardatescalendar(year, width)

Parameter: 
year: year of the calendar
width: [Default: 3] number of months in each row. 

Returns: a list of month rows.

代码#1:

Python3
# Python program to demonstrate working
# of yeardatescalendar() method
 
# importing calendar module
import calendar
 
obj = calendar.Calendar()
 
year = 2016
# default value of width is 3
 
# printing with yeardatescalendar
print(obj.yeardatescalendar(year))


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


输出:


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

Python3

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

输出: