calendar

1. calendar.calendar(year, w, l, c, m)

  • Returns a year's calendar as a multi-line string.
>>> 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.calendar(year, w=2, l=1, c=6, m=3)
    • 返回一个多行字符串格式的 year 年年历
    • w 为日期列宽度的字符数(因为日子有单数、双数,所以一般为 2)
    • l 为每周行数(若设为 2,就是头一行显示字符,下一行空着;数字增大,下方空行随之增多)
    • c 为月份之间的空格数
    • m 为每行显示的月份数

2. calendar.prcal(year, w, l, c, m)

  • Print a year's calendar.
  • w, l, c, m 的用法同 calendar.calendar(year, w, l, c, m)
  • calendar.prcalcalendar.calendar 的参数默认值有所不同
    • calendar.prcal(year, w=0, l=0, c=6, m=3)
    • calendar.calendar(year, w=2, l=1, c=6, m=3)
# 下方两句结果相同
>>> print(calendar.calendar(2019))
...
>>> calendar.prcal(2019)
...

3. 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

4. calendar.prmonth(year, month, w, l)

  • Print a month's calendar.
  • w, l 的用法同 calendar.calendar(year, w, l, c, m)
  • calendar.prmonthcalendar.calendar 的参数默认值有所不同
    • calendar.prmonth(year, month, w=0, l=0)
    • calendar.calendar(year, w=2, l=1, c=6, m=3)
# 下方两句结果相同
>>> print(calendar.month(2019, 12))
...
>>> calendar.prmonth(2019, 12)
...

5. calendar.isleap(year)

  • Return True for leap years, False for non-leap years.
>>> calendar.isleap(2000)
True
>>> calendar.isleap(2019)
False

6. 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

7. 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]]

8. calendar.monthrange(year, month)

  • Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.
>>> calendar.monthrange(2019, 12)
(6, 31)

9. 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
12-19 14:41