Python|日历模块
Python定义了一个内置模块calendar来处理与日历相关的操作。
日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。默认情况下,这些日历将星期一作为一周的第一天,将星期日作为最后一天(欧洲惯例)。
示例 #1:显示给定月份的日历。
Python3
# 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))
Python3
# Python code to demonstrate the working of
# calendar() function to print calendar
# importing calendar module
# for calendar operations
import calendar
# using calendar to print calendar of year
# prints calendar of 2018
print ("The calendar of year 2018 is : ")
print (calendar.calendar(2018, 2, 1, 6))
输出:
示例 #2:显示给定年份的日历。
Python3
# Python code to demonstrate the working of
# calendar() function to print calendar
# importing calendar module
# for calendar operations
import calendar
# using calendar to print calendar of year
# prints calendar of 2018
print ("The calendar of year 2018 is : ")
print (calendar.calendar(2018, 2, 1, 6))
输出:
类 calendar.Calendar :
日历类创建一个日历对象。 Calendar 对象提供了几种方法,可用于准备日历数据以进行格式化。这个类本身不做任何格式化。这是子类的工作。 Calendar 类允许根据日期、月份和年份计算各种任务。 Calendar 类提供以下方法:
Function Description iterweekdays() Returns an iterator for the week day numbers that will be used for one week itermonthdates() Returns an iterator for the month (1–12) in the year itermonthdays() Returns an iterator of a specified month and a year itermonthdays2() Method is used to get an iterator for the month in the year similar to itermonthdates(). Days returned will be tuples consisting of a day of the month number and a week day number. itermonthdays3() Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month and a day of the month numbers. itermonthdays4() Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month, a day of the month, and a day of the week numbers. monthdatescalendar() Used to get a list of the weeks in the month of the year as full weeks monthdays2calendar() Used to get a list of the weeks in the month of the year as full weeks monthdayscalendar Used to get a list of the weeks in the month of the year as full weeks yeardatescalendar() Used to get a list of the weeks in the month of the year as full weeks yeardays2calendar() Used to get the data for specified year. Entries in the week lists are tuples of day numbers and weekday numbers yeardayscalendar() Used to get the data for specified year. Entries in the week lists are day numbers
类 calendar.TextCalendar :
TextCalendar 类可用于生成纯文本日历。 Python中的 TextCalendar 类允许您编辑日历并根据需要使用它。
Function Description formatmonth() Method is used to get month’s calendar in a multi-line string prmonth() Method is used to print a month’s calendar as returned by formatmonth() formatyear() Method is used to get m-column calendar for an entire year as a multi-line string pryear() Method is used to print the calendar for an entire year as returned by formatmonth()
类 calendar.HTMLCalendar :
HTMLCalendar 类可用于生成 HTML 日历。 Python中的 HTMLCalendar 类允许您编辑日历并根据您的要求使用。
Function Description formatmonth() Method is used to get month’s calendar as an HTML table formatyear() Method is used to get year’s calendar as an HTML table. formatyearpage() Method is used to get year’s calendar as a complete HTML page
简单的 TextCalendar 类:
对于简单的文本日历日历模块提供以下功能:
Function Description setfirstweekday() Function sets the day start number of week firstweekday() Function returns the first week day number. By default 0 (Monday) isleap() Function checks if year mentioned in argument is leap or not leapdays() Function returns the number of leap days between the specified years in arguments weekday() Function returns the week day number(0 is Monday) of the date specified in its arguments weekheader() Returns a header containing abbreviated weekday names monthrange() Function returns two integers, first, the starting day number of week(0 as monday), second, the number of days in the month monthcalendar() Returns a matrix representing a month’s calendar. Each row represents a week; days outside of the month are represented by zeros prmonth() Function also prints the month of specific year but there is no need of “print” operation to execute this month() Function prints the month of a specific year mentioned in arguments prcal() Function also prints the calendar of specific year but there is no need of “print” operation to execute this calendar() Function displays the year, width of characters, no. of lines per week and column separations.