Python标准库 - calendar

310 阅读10分钟

Python 的标准函数“calendar”提供处理日期相关的实用方法,同时也可以将日历输出成为常见的日历格式。

calendar 常用方法

下方列出几种 calendar 模块常用的方法 :

方法参数说明
Calendar()
创建一个 calendar 对象
TextCalendar()
创建一个可以打印出与使用的日历
HTMLCalendar()
创建一个包含 HTML 格式的日历
setfirstweekday()n设置日历里,每个星期的第一天是星期几 ( 星期一为 0,星期天为 6 )
firstweekday()
返回每个星期第一天的数值
isleap()year判断 year 年份是否为闰年,返回 True 或 False
leapdays()y1, y2计算 y1 年到 y2 年之间共包含几个闰年
weekday()year, month, day返回某年某月的某一天是星期几 ( 星期一是 0 )
weekheader()n返回星期几的开头缩写,n 的范围是 1~3
monthrange()year, month返回某年某月的第一天是星期几 ( 星期一为 0 ),以及这个月的天数
monthcalendar()year, month返回一个日历的二维列表
month()year, month, w=0, l=0返回一个格式化的月份字符串
prmonth()year, month, w=0, l=0等同 print(calendar.month())
calendar()year, w=0, l=0, c=6, m=3返回一个格式化后一整年的月历字符串
prcal()year, w=2, l=1, c=6, m=3等同 print(calendar.calendar())
day_name、calendar.day_abbr
返回星期一到星期天的名称或缩写
month_name、calendar.month_abbr
返回 1~12 月的名称或缩写

import calendar

要使用 calendar 必须先 import calendar 模块,或使用 from 的方式,单独 import 特定的类型。

import calendar
from calendar import prmonth

Calendar()

calendar.Calendar() 可以创建一个 calendar 对象,calendar 对象可以使用下列几种方法:

方法说明
iterweekdays()产生一星期 0~6 的迭代对象
itermonthdates(year, month)产生某年某月的迭代对象,如果该月份不是从星期一开始,会自动上前一个月的后几天,datetime.date
itermonthdays(year, month)产生一个月份的迭代对象,格式为 0~6,如果该月份不是从星期一开始,会自动补 0
itermonthdays2(year, month)产生一个月份的迭代对象,格式为 (0,0)~(31,6),如果该月份不是从星期一开始,会自动补 0
monthdatescalendar(year, month)产生一个月份的二维列表,每个子列表由一周的七天组成,格式为 datetime.date
monthdayscalendar(year, month)产生一个月份的二维列表,每个子列表由一周的七天组成,格式为 0~6
monthdays2calendar(year, month)产生一个月份的二维列表,每个子列表由一周的七天组成,格式为 (0,0)~(31,6)
yeardatescalendar(year)产生一整年的多维列表,每个子列表由一周的七天组成,格式为 datetime.date
yeardays2calendar(year)产生一整年的多维列表,每个子列表由一周的七天组成,格式为 0~6
yeardayscalendar(year)产生一整年的多维列表,每个子列表由一周的七天组成,格式为 (0,0)~(31,6)
import calendar
c = calendar.Calendar()
print(list(c.iterweekdays()))
#[0, 1, 2, 3, 4, 5, 6]
print(list(c.itermonthdates(2021,10)))
# [datetime.date(2021927)datetime.date(2021928)....
print(list(c.itermonthdays(2021,10)))
# [0, 0, 0, 0, 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]
print(list(c.itermonthdays2(2021,10)))
# [(00)(01)(02)(03)(14)(25).....
print(c.monthdatescalendar(2021,10))
# [[datetime.date(2021927)datetime.date(2021928).....
print(c.monthdayscalendar(2021,10))
# [[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10]......
print(c.monthdays2calendar(2021,10))
# [[(00)(01)(02)(03)(14)(25).....
print(c.yeardatescalendar(2021))
# [[[[datetime.date(20201228)datetime.date(20201229)....
print(c.yeardayscalendar(2021))
# [[[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10]....
print(c.yeardays2calendar(2021))
# [[[[(00)(01)(02)(03)(14)(25)....

TextCalendar()

calendar.TextCalendar() 可以创建一个可以打印出与使用的日历,有下列几种方法:

方法说明
formatmonth(year, month, w=0, l=0)产生格式化后一个月份的日历,w 和 l 是显示的宽高 ( 可不填 )
prmonth(year, month, w=0, l=0)等同 print(formatmonth)
formatyear(year, month, w=0, l=0, c=6, m=3)产生格式化后一个年份的月历,w 和 l 是显示的宽高 ( 可不填 ),c 和 m 表示是垂直和水平显示的列与栏 ( 可不填 )
pryear(year, month, w=0, l=0, c=6, m=3)等同 print(formatyear)
import calendar
tc = calendar.TextCalendar()
print(tc.formatmonth(2021,10))
tc.prmonth(2021,10)
'''
    October 2021
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
'''
print(tc.formatyear(2021, c=3, m=4))
tc.pryear(2021, c=3, m=4)
'''
                                           2021
      January                February                March                  April
Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
             1  2  3    1  2  3  4  5  6  7    1  2  3  4  5  6  7             1  2  3  4
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    8  9 10 11 12 13 14    5  6  7  8  9 10 11
11 12 13 14 15 16 17   15 16 17 18 19 20 21   15 16 17 18 19 20 21   12 13 14 15 16 17 18
18 19 20 21 22 23 24   22 23 24 25 26 27 28   22 23 24 25 26 27 28   19 20 21 22 23 24 25
25 26 27 28 29 30 31                          29 30 31               26 27 28 29 30
        May                    June                   July                  August
Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
                1  2       1  2  3  4  5  6             1  2  3  4                      1
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    5  6  7  8  9 10 11    2  3  4  5  6  7  8
10 11 12 13 14 15 16   14 15 16 17 18 19 20   12 13 14 15 16 17 18    9 10 11 12 13 14 15
17 18 19 20 21 22 23   21 22 23 24 25 26 27   19 20 21 22 23 24 25   16 17 18 19 20 21 22
24 25 26 27 28 29 30   28 29 30               26 27 28 29 30 31      23 24 25 26 27 28 29
31                                                                   30 31
    September               October                November               December
Mo Tu We Th Fr Sa Su   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          1  2  3  4  5
 6  7  8  9 10 11 12    4  5  6  7  8  9 10    8  9 10 11 12 13 14    6  7  8  9 10 11 12
13 14 15 16 17 18 19   11 12 13 14 15 16 17   15 16 17 18 19 20 21   13 14 15 16 17 18 19
20 21 22 23 24 25 26   18 19 20 21 22 23 24   22 23 24 25 26 27 28   20 21 22 23 24 25 26
27 28 29 30            25 26 27 28 29 30 31   29 30                  27 28 29 30 31
'''

HTMLCalendar()

calendar.HTMLCalendar() 可以创建一个包含 HTML 格式的日历,,有下列几种方法:

方法说明
formatmonth(year, month, withyear=True)输出某年里某个月份的日历 HTML 表格,withyear 默认 True 表示显示年份 ( 可不填 )
formatmonth(year, width=3)输出某年的月历 HTML 表格,width 表示一行里有几个月,默认 3 ( 可不填 )
formatyearpage(year, width=3, css='xxx.css', encoding=None)输出某年的月历 HTML 网页代码,width 表示一行里有几个月,默认 3 ( width、css、encoding 都可不填 )
import calendar
html = calendar.HTMLCalendar()
print(html.formatmonth(2021,10))
# 示例网页:https://jsbin.com/jilexovube/1/edit?html,css,output

图片

import calendar
html = calendar.HTMLCalendar()
print(html.formatyear(2021, width=4))
print(html.formatyearpage(2021, width=4))
# 示例网址:https://jsbin.com/nawepogoti/1/edit?html,css,output

图片

setfirstweekday(n)

calendar.setfirstweekday(n) 可以设置日历里,每个星期的第一天是星期几 ( 星期一为 0,星期天为 6 ),只要是日期对象都可以使用 setfirstweekday 方法设置,例如下方的程序,执行后会将星期五变成日历的第一天。

import calendar
tc = calendar.TextCalendar()
tc.setfirstweekday(4)    # 设置星期五为第一天
tc.prmonth(2021,10)
'''
    October 2021
Fr Sa Su Mo Tu We Th
 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
'''

firstweekday()

calendar.firstweekday() 使用后会返回每个星期第一天的数值,如果设置为 0,就会返回 0。

import calendar
calendar.setfirstweekday(0)
print(calendar.firstweekday())   # 0

isleap(year)

calendar.isleap(year) 可以判断某一年是否为闰年,返回 True 或 False。

import calendar
print(calendar.isleap(2020))   # True
print(calendar.isleap(2021))   # False
print(calendar.isleap(2022))   # False

leapdays(y1, y2)

calendar.leapdays(y1, y2) 可以计算 y1 年到 y2 年之间共包含几个闰年。

import calendar
print(calendar.leapdays(19202020))   # 25 ( 1920~2020 年间,有 25 个闰年 )

weekday(year, month, day)

calendar.weekday(year, month, day) 可以返回某年某月的某一天是星期几 ( 星期一从 0 开始 )。

import calendar
print(calendar.weekday(2021,10,1))   # 4 ( 2021/10/1 是星期五 )

weekheader(n)

calendar.weekheader(n) 可以返回星期几的开头缩写,n 的范围是 1~3。

import calendar
print(calendar.weekheader(1))   # M T W T F S S
print(calendar.weekheader(2))   # Mo Tu We Th Fr Sa Su
print(calendar.weekheader(3))   # Mon Tue Wed Thu Fri Sat Sun

monthrange(year, month)

calendar.monthrange(year, month) 可以返回某年某月的第一天是星期几 ( 星期一为 0 ),以及这个月的天数。

import calendar
print(calendar.monthrange(2021,10))
# (431)  2021 的 10 月有 31 天,10 月第一天是星期五
print(calendar.monthrange(2021,11))   # (030)
# (030)  2021 的 11 月有 30 天,11 月第一天是星期一

monthcalendar(year, month)

calendar.monthcalendar(year, month) 可以返回一个日历的二维列表。

import calendar
print(calendar.monthcalendar(2021,10))
# [[0, 0, 0, 0, 1, 2, 3][4, 5, 6, 7, 8, 9, 10][11, 12, 13, 14, 15, 16, 17]...

month(year, month, w=0, l=0)

calendar.month(year, month, w=0, l=0) 可以返回一个格式化的月份字符串,效果和 formatmonth 相同。

import calendar
calendar.setfirstweekday(6)    # 设置第一天是星期天
print(calendar.month(202110))
'''
    October 2021
Su Mo Tu We Th Fr Sa
                 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
'''

prmonth(year, month, w=0, l=0)

效果等同 print(calendar.month(year, month, w=0, l=0))

calendar(year, w=0, l=0, c=6, m=3)

效果等同 formatyear(year, month, w=0, l=0, c=6, m=3)

prcal(year, w=2, l=1, c=6, m=3)

效果等同 print(calendar.calendar(year, w=0, l=0, c=6, m=3))

day_name, calendar.day_abbr

calendar.day_name、calendar.day_abbr 使用后会返回星期一到星期天的名称或缩写。

import calendar
print(list(calendar.day_name))
# ['Monday''Tuesday''Wednesday''Thursday''Friday''Saturday''Sunday']
print(list(calendar.day_abbr))
# ['Mon''Tue''Wed''Thu''Fri''Sat''Sun']

month_name, calendar.month_abbr

calendar.month_name、calendar.month_abbr 使用后会返回 1~12 月的名称或缩写 ( 产生后的第一个值会是空值 )。

import calendar
print(list(calendar.month_name))
# ['''January''February''March''April''May''June''July''August''September''October''November''December']
print(list(calendar.month_abbr))
# ['''Jan''Feb''Mar''Apr''May''Jun''Jul''Aug''Sep''Oct''Nov''Dec']