Python干货宝典!玩转内置模块:日历模块

505 阅读1分钟

Python定义了内置模块calendar它处理与日历相关的操作。

Calendar module允许输出日历(如程序),并提供与日历相关的其他有用功能。

Calendar模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限期扩展。

默认情况下,这些日历的周一为一周的第一天,星期日为最后一天。

**例1:**显示给定月份的日历。

# Python program to display calendar of 
# given month of the year 
    
# import module 
import calendar 
    
yy = 2017
mm = 11
    
# display the calendar 
print(calendar.month(yy, mm)) 

产出:

**例2:**显示给定年份的日历。

# Python code to demonstrate the working of 
# calendar() function to print calendar
    
# importing calendar module 
# for calendar operations 
import calendar 
    
# using calender to print calendar of year 
# prints calendar of 2018 
print ("The calender of year 2018 is : ") 
print (calendar.calendar(2018, 2, 1, 6)) 

产出:

class calendar.Calendar :

类创建Calendar对象。Calendar对象提供了几种可用于准备日历数据以进行格式化的方法。

日历类允许根据日期、月份和年份计算各种任务。日历类提供以下方法:

周列表中的条目是天数。

class calendar.TextCalendar :
TextCalendar类可用于生成纯文本日历。Python中的TextCalendar类允许您根据需要编辑日历并使用。

class calendar.HTMLCalendar :
HTMLCalendar类可用于生成HTML日历。Python中的HTMLCalendar类允许您根据需要编辑日历并使用。

Simple TextCalendar class :

对于简单的文本日历

日历模块

提供下列功能: