calendar.calendar(year, w, l, c, m)
释义
- Returns a year's calendar as a multi-line string.
calendar.calendar(year, w=2, l=1, c=6, m=3)
- 返回一个多行字符串格式的
year
年年历
w
为日期列宽度的字符数(因为日子有单数、双数,所以一般为 2)
l
为每周行数(数字增大,可以认为行间距增大)
c
为月份之间的空格数
m
为每行显示的月份数
举例
>>> import calendar
>>>
>>> cal = calendar.calendar(2019)
>>> print(type(cal))
<class 'str'>
>>> print(cal)
2019
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 6 1 2 3 1 2 3
7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10
14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17
21 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24
28 29 30 31 25 26 27 28 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 6 7 1 2 3 4 5 1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23
29 30 27 28 29 30 31 24 25 26 27 28 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 6 7 1 2 3 4 1
8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8
15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15
22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22
29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29
30
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 5 6 1 2 3 1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22
28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29
30 31
>>>
calendar.prcal(year, w, l, c, m)
释义
- Print a year's calendar.
w
, l
, c
, m
的用法同 calendar.calendar(year, w, l, c, m)
calendar.prcal
与 calendar.calendar
的参数默认值有所不同
calendar.prcal(year, w=0, l=0, c=6, m=3)
calendar.calendar(year, w=2, l=1, c=6, m=3)
- 这个方法是打印,不返回字符串
举例
>>> calendar.prcal(2019)
...
>>>
calendar.month(year, month, w, l)
释义
- Return a month's calendar string (multi-line).
w
, l
的用法同 calendar.calendar(year, w, l, c, m)
举例
>>> m12 = calendar.month(2019, 12)
>>> print(m12)
December 2019
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
>>>
calendar.prmonth(year, month, w, l)
释义
- Print a month's calendar.
w
, l
的用法同 calendar.calendar(year, w, l, c, m)
- 这个方法是打印,不返回字符串
举例
>>> calendar.prmonth(2019, 12)
...
>>>
calendar.isleap(year)
释义
- Return True for leap years, False for non-leap years.
举例
>>> calendar.isleap(2000)
True
>>> calendar.isleap(2019)
False
>>>
calendar.leapdays(y1, y2)
释义
- Return number of leap years in range [y1, y2).
- Assume y1 <= y2.
举例
>>> calendar.leapdays(2000, 2019)
5
>>> calendar.leapdays(2019, 2000)
-5
>>> calendar.leapdays(2000, 2020)
5
>>>
calendar.monthcalendar(year, month)
释义
- Return a matrix representing a month's calendar.
- Each row represents a week; days outside this month are zero.
举例
>>> m12 = calendar.monthcalendar(2019, 12)
>>> type(m12)
<class 'list'>
>>> m12
[[0, 0, 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, 0, 0, 0, 0, 0]]
>>> for week in m12:
print("{:2} {:2} {:2} {:2} {:2} {:2} {:2}".format(*week))
0 0 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 0 0 0 0 0
>>>
calendar.monthrange(year, month)
释义
- Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.
- 返回两个数,第一个数是该月 1 号的星期数,第二个是该月天数
举例
>>> calendar.monthrange(2019, 12)
(6, 31)
>>>
calendar.weekday(year, month, day)
释义
- Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).
举例
>>> calendar.weekday(2019, 12, 1)
6
>>>
其他
calendar.firstweekday()
- 返回当前每周起始日期的设置
- 默认情况下,首次载入
caendar
模块时返回 0,即星期一
calendar.setfirstweekday(weekday)
calendar.timegm((year, month, day, hour, minute, second))
- 与
time.gmtime()
相反
- 接受一个时间元组形式,返回该时刻的时间戳(1970.1.1 至今经过的浮点秒数)