python 示例
Python中的日历模块 (calendar Module in Python)
The methods and classes defined in this module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. Zero and negative years are interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is 2 BC.
此模块中定义的方法和类使用理想化的日历,当前的公历在两个方向上无限期扩展。 零年和负年按照ISO 8601标准的规定进行解释。 0年是1 BC,-1年是2 BC。
These calendars have Monday as the first day of the week, and Sunday as the last, by default. You can use the setfirstweekday() to set the first day of the week to Sunday (6) or any other weekday. Parameters that specify dates are given as integers.
这些日历默认情况下以星期一为一周的第一天,以星期日为最后一天。 您可以使用setfirstweekday()将一周的第一天设置为周日(6)或任何其他工作日。 指定日期的参数以整数形式给出。
Example: Python program to display the calendar of a given year.
示例:显示给定年份日历的Python程序。
# Python program to print the calendar of
# the given year
# importing calendar module
import calendar
# using calendar to print calendar of year
print ("The calender of year 2020 is : ")
print (calendar.calendar(2020, 2, 1, 6))
Output:
输出:
The calender of year 2020 is :
2020
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1
6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29
30 31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
31
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
Example: Python program to display a given month.
示例:显示给定月份的Python程序。
# Python program to print the calendar of
# the given month
# importing calendar module
import calendar
# using calendar to print calendar of May 2020
print ("The calender of May 2020 is : ")
calendar.prmonth(2020, 5)
Output:
输出:
The calender of May 2020 is :
May 2020
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
A.日历模块中的类和方法 (A. Classes and methods in calendar module)
Calendar module in Python provides the following classes,
Python中的日历模块提供以下类,
1.日历班 (1. Calendar class)
This class creates a Calendar object. A Calendar object provides several methods that can be used for preparing the calendar data for formatting. The formatting of data is done by the subclasses of this class.
此类创建Calendar对象。 Calendar对象提供了几种可用于准备日历数据以进行格式化的方法。 数据的格式化由此类的子类完成。
Method | Description |
---|---|
iterweekdays() | Returns an iterator for the week day numbers which will be used for one week. |
itermonthdates(y, m) | Returns an iterator for the month m (1–12) in the year y. |
itermonthdays(y, m) | Returns an iterator for the month m in the year y similar to itermonthdates(). Days returned are the day of the month numbers. For the days outside of the given month, the day number is 0. |
itermonthdays2(y, m) | Returns an iterator for the month m in the year y Days returned are tuples consisting of a day of the month number and a weekday number. |
itermonthdays3(y, m) | Returns an iterator for the month m in the year y Days returned are tuples consisting of a year, a month and a day of the month numbers. Available from version 3.7 |
itermonthdays4(y, m) | Returns an iterator for the month m in the year y Days returned are tuples consisting of a year, a month, a day of the month, and a day of the week numbers. Available from version 3.7 |
monthdatescalendar(y, m) | Returns a list of the weeks in the month m of the year y as full weeks. Weeks given as lists of seven datetime.date objects. |
monthdays2calendar(y, m) | Returns a list of the weeks in the month m of the year y as full weeks. Weeks given as lists of seven tuples of day numbers and weekday numbers. |
monthdayscalendar(y, m) | Returns a list of the weeks in the month m of the year y as full weeks. Weeks given as lists of seven-day numbers. |
yeardatescalendar(y, width=3) | Returns the data for the specified year y ready for formatting. The return value is a list of month rows. |
yeardays2calendar(y, width=3) | Returns the data for the specified year y ready for formatting. Week lists are tuples of day numbers and weekday numbers. |
yeardayscalendar(y, width=3) | Returns the data for the specified year y ready for formatting. Week lists given are lists of day numbers. |
方法 | 描述 |
---|---|
iterweekdays() | 返回将用于一周的星期几的迭代器。 |
itermonthdates(y,m) | 返回y年中m(1–12)个月的迭代器。 |
itermonthdays(y,m) | 返回y年中月m的迭代器,类似于itermonthdates()。 返回的天数是月份中的天数。 对于给定月份以外的日期,天数为0。 |
itermonthdays2(y,m) | 返回年份y中月份m的迭代器 返回的天是元组,它由一个月中的某天的数字和一个工作日的数字组成。 |
itermonthdays3(y,m) | 返回y年中月份m的迭代器。返回的天是由年份,月份和月份中的日期组成的元组。 从3.7版开始可用 |
itermonthdays4(y,m) | 返回y年中第m个月的迭代器。返回的天是元组,由年,月,月中的某天和星期几组成。 从3.7版开始可用 |
monthdatescalendar(y,m) | 返回y年的m个月中的星期列表(以完整的星期为单位)。 以七个datetime.date对象的列表形式给出的周数。 |
monthdays2calendar(y,m) | 返回y年的m个月中的星期列表(以完整的星期为单位)。 给出的星期数是日数和工作日数的七个元组的列表。 |
monthdayscalendar(y,m) | 返回y年的m个月中的星期列表(以完整的星期为单位)。 以7天数字列表形式给出的周数。 |
yeardatescalendar(y,width = 3) | 返回指定年份y的数据,准备进行格式化。 返回值是月份行的列表。 |
yeardays2calendar(y,width = 3) | 返回指定年份y的数据,准备进行格式化。 周列表是日编号和工作日编号的元组。 |
yeardayscalendar(y,width = 3) | 返回指定年份y的数据,准备进行格式化。 给定的周列表是天数列表。 |
2. TextCalendar类 (2. TextCalendar Class)
This class creates simple text calendars.
此类创建简单的文本日历。
Method | Description |
---|---|
formatmonth(year, month, w=0, l=0) | Returns a month's calendar in a multi-line string. |
prmonth(year, month, w=0, l=0) | Prints a month's calendar as returned by formatmonth(). |
formatyear(year, w=2, l=1, c=6, m=3) | Returns a m-column calendar for an entire year as a multi-line string. |
pryear(year, w=2, l=1, c=6, m=3) | Prints the calendar for an entire year as returned by formatyear(). |
方法 | 描述 |
---|---|
格式month(年,月,w = 0,l = 0) | 以多行字符串返回一个月的日历。 |
prmonth(年,月,w = 0,l = 0) | 打印由formatmonth()返回的月份的日历。 |
formatyear(年,w = 2,l = 1,c = 6,m = 3) | 以多行字符串的形式返回整个一年的m列日历。 |
pryear(年,w = 2,l = 1,c = 6,m = 3) | 打印格式为year()的整年的日历。 |
3. HTMLCalendar类 (3. HTMLCalendar Class)
This class is used to create HTML calendars and do operations on it. HTML Calendar has following methods and attributes:
此类用于创建HTML日历并对其进行操作。 HTML日历具有以下方法和属性:
Methods
方法
Method | Description |
---|---|
formatmonth(year, month, withyear=True) | Returns a month's calendar as an HTML table. |
formatyear(year, width=3) | Returns a year's calendar as an HTML table. |
formatyearpage(year, width=3, css='calendar.css', encoding=None) | Returns a year's calendar as a complete HTML page. |
方法 | 描述 |
---|---|
格式month(年,月,withyear = True) | 以HTML表格的形式返回一个月的日历。 |
格式年份(年,宽度= 3) | 以HTML表格的形式返回一年的日历。 |
formatyearpage(year,width = 3,css ='calendar.css',encoding = None) | 返回一年的日历作为完整HTML页面。 |
Attributes
属性
Attribute | Description |
---|---|
cssclasses | List of CSS classes used for each weekday. |
cssclass_noday | CSS class for a weekday occurring in the previous or coming month. |
cssclasses_weekday_head | List of CSS classes used for weekday names in the header row. |
cssclass_month_head | The month's head CSS class (used by formatmonthname()). The default value is "month". |
cssclass_month | CSS class for the whole month's table (used by formatmonth()). The default value is "month". |
cssclass_year | CSS class for the whole year's table of tables (used by formatyear()). The default value is "year". |
cssclass_year_head | CSS class for the table head for the whole year (used by formatyear()). The default value is "year". |
属性 | 描述 |
---|---|
CSS类 | 每个工作日使用CSS类列表。 |
cssclass_noday | 上个月或下个月发生的工作日CSS类。 |
cssclasses_weekday_head | 标头行中用于工作日名称CSS类列表。 |
cssclass_month_head | 本月的头CSS类(由formatmonthname()使用)。 默认值为“月”。 |
cssclass_month | 整个月表CSS类(由formatmonth()使用)。 默认值为“月”。 |
cssclass_year | 全年表CSS类(由formatyear()使用)。 默认值为“年份”。 |
cssclass_year_head | 全年表头CSS类(由formatyear()使用)。 默认值为“年份”。 |
4. LocaleTextCalendar类 (4. LocaleTextCalendar Class)
This is a subclass of TextCalendar class. It can be passed a locale name in the constructor which will return month and weekday names in the specified locale.
这是TextCalendar类的子类。 可以在构造函数中为其传递语言环境名称,该名称将在指定的语言环境中返回月份和工作日的名称。
5. LocaleHTMLCalendar CLass (5. LocaleHTMLCalendar CLass)
This is a subclass of HTMLCalendar class. It can be passed a locale name in the constructor which will return month and weekday names in the specified locale.
这是HTMLCalendar类的子类。 可以在构造函数中为其传递语言环境名称,该名称将在指定的语言环境中返回月份和工作日的名称。
Calendar module in Python provides the following methods for simple text calendars:
Python中的日历模块为简单的文本日历提供了以下方法:
Methods | Description |
---|---|
setfirstweekday(weekday) | Sets the weekday to start each week. Where (0 is Monday, 6 is Sunday). |
firstweekday() | Returns the current setting for the weekday to start each week. |
isleap(y) | Returns True if y is a leap year, else False. |
leapdays(y1, y2) | Returns the number of leap years in the range from y1 to y2 , where y1 and y2 are years. |
weekday(year, month, day) | Returns the day of the week (0 is Monday) for year (1970–...), month (1–12), day (1–31). |
weekheader(n) | Return a header containing abbreviated weekday names. n specifies the width in characters for one weekday. |
monthrange(year,month) | Returns weekday of first day of the month and number of days in month, for the given year and month. |
monthcalendar(year, month) | Returns a matrix representing a month's calendar. Each row represents a week; days outside of the month are printed 0. |
prmonth(year, month, w=0, l=0) | Prints a month's calendar as returned by month(). |
month(year, month, w=0, l=0) | Returns a month's calendar in a multi-line string using the formatmonth() of the TextCalendar class. |
prcal(year, w=0, l=0, c=6, m=3) | Prints the calendar for an entire year as returned by calendar(). |
calendar(year, w=0, l=0, c=6, m=3) | Returns a 3-column calendar for an entire year as a multi-line string using the formatyear() of the TextCalendar class. |
方法 | 描述 |
---|---|
setfirstweekday(工作日) | 将工作日设置为每周开始。 其中(0是星期一,6是星期日)。 |
firstweekday() | 返回每个星期开始的工作日的当前设置。 |
isleap(y) | 如果y是a年,则返回True,否则返回False。 |
日(y1,y2) | 返回从y1到y2的of年数,其中y1和y2是年。 |
工作日(年,月,日) | 返回年份(1970 –...),月份(1–12),日期(1–31)的星期几(0为星期一)。 |
周头 | 返回包含缩写的工作日名称的标头。 n指定一个工作日的宽度(以字符为单位)。 |
月范围(年,月) | 返回给定年份和月份的月份的第一天的工作日以及月份中的天数。 |
月历(年,月) | 返回代表一个月日历的矩阵。 每行代表一周; 该月以外的天数打印为0。 |
prmonth(年,月,w = 0,l = 0) | 打印month()返回的月份日历。 |
month(year,month,w = 0,l = 0) | 使用TextCalendar类的formatmonth()以多行字符串返回一个月的日历。 |
prcal(年,w = 0,l = 0,c = 6,m = 3) | 打印由calendar()返回的整年的日历。 |
日历(年,w = 0,l = 0,c = 6,m = 3) | 使用TextCalendar类的格式year()以多行字符串形式返回整个一年的3列日历。 |
B.日历模块中的数据属性 (B. Data attributes in the calendar module)
This module has the following data attributes:
该模块具有以下数据属性:
day_name: It is an array that represents the days of the week in the current locale.
day_name :这是一个数组,表示当前语言环境中的星期几。
day_abbr: It is an array that represents the abbreviated days of the week in the current locale.
day_abbr :这是一个数组,表示当前语言环境中星期几的缩写。
month_name: It is an array that represents the months of the year in the current locale. Here January is represented as month number 1, so the array has a length of 13 and month_name[0] being an empty string.
month_name :这是一个数组,表示当前语言环境中的一年中的月份。 在这里,一月表示为月份号1,因此该数组的长度为13,而month_name [0]为空字符串。
month_abbr: It is an array that represents the abbreviated months of the year in the current locale. Here January is represented as month number 1, so the array has a length of 13 and month_abbr[0] being an empty string.
month_abbr :这是一个数组,表示当前语言环境中一年中的缩写月份。 这里的一月表示为月份号1,因此数组的长度为13,而month_abbr [0]为空字符串。
Reference: calendar — General calendar-related functions
参考: 日历—与日历相关的常规功能
翻译自: https://www.includehelp.com/python/calendar-module-with-examples.aspx
python 示例