python中calendar

Calendar

前言

calendar模块与日历相关.

使用

打印某年某月

#!/usr/bin/env python3
import calendar


print(calendar.month(2018, 10))
    October 2018
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.calendar(year, w=2, l=1, c=6)

>>> print(calendar.calendar(2018, w=2, l=1, c=6))
                                  2018

      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  7                1  2  3  4                1  2  3  4
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       5  6  7  8  9 10 11
15 16 17 18 19 20 21      12 13 14 15 16 17 18      12 13 14 15 16 17 18
22 23 24 25 26 27 28      19 20 21 22 23 24 25      19 20 21 22 23 24 25
29 30 31                  26 27 28                  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          1  2  3  4  5  6                   1  2  3
 2  3  4  5  6  7  8       7  8  9 10 11 12 13       4  5  6  7  8  9 10
 9 10 11 12 13 14 15      14 15 16 17 18 19 20      11 12 13 14 15 16 17
16 17 18 19 20 21 22      21 22 23 24 25 26 27      18 19 20 21 22 23 24
23 24 25 26 27 28 29      28 29 30 31               25 26 27 28 29 30
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             1  2  3  4  5                      1  2
 2  3  4  5  6  7  8       6  7  8  9 10 11 12       3  4  5  6  7  8  9
 9 10 11 12 13 14 15      13 14 15 16 17 18 19      10 11 12 13 14 15 16
16 17 18 19 20 21 22      20 21 22 23 24 25 26      17 18 19 20 21 22 23
23 24 25 26 27 28 29      27 28 29 30 31            24 25 26 27 28 29 30
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  5  6  7                1  2  3  4                      1  2
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       3  4  5  6  7  8  9
15 16 17 18 19 20 21      12 13 14 15 16 17 18      10 11 12 13 14 15 16
22 23 24 25 26 27 28      19 20 21 22 23 24 25      17 18 19 20 21 22 23
29 30 31                  26 27 28 29 30            24 25 26 27 28 29 30
                                                    31

w=2, l=1, c=6: 都是字符或行间隔设置.

calendar.calendar: 返回某年的日历表.

calendar.firstweekday()

>>> calendar.firstweekday()
0

calendar.firstweekday(): 返回当前每周起始日期的设置. 默认情况下, 返回0即周一.

calendar.isleap(year)

>>> print(calendar.isleap(2016))
True

calendar.isleap: 判断某年是否为润年, 返回bool.

calendar.leapdays(y1, y2)

>>> print(calendar.leapdays(2014, 2016))
0

calendar.leapdays: 返回指定年份范围内闰年的总数, 左闭右开性质.

calendar.month(year, month, w=2, l=1)

>>> print(calendar.month(2018, 10, w=2, l=1))
    October 2018
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.month(year, month, w=2, l=1): 返回指定月份的日历表, w,l 表示间隔.

calendar.monthcalendar(yaer, month)

>>> print(calendar.monthcalendar(2018, 10))
[[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]]

calendar.monthcalendar: 指定月份, 返回该月每周的日期号, 以嵌套的列表形式显示.

calendar.monthrange(year, month)

>>> print(calendar.monthrange(2018, 10))
(0, 31)

calendar.monthrange: 指定月份, 返回tuple的两个数, 第一个表示该月第一天是周几, 第二个表示该月有多少天.

calendar.setfirstweekday(weekday)

>>> print(calendar.setfirstweekday(1))
None
>>> print(calendar.firstweekday())
1

calendar.setfirstweekday: 将每周的起始序号改为指定数值. 原为0开始.

calendar.weekday(year, month, day)

>>> print(calendar.weekday(2018, 10, 8))
0

calendar.weekday: 返回指定年月日的星期码即周几.


其他

  • calendar.prcal(year, w=2, l=1, c=6)
  • calendar.prmonth(year, month, w=2, l=1)
  • calendar.timegm(tupletime)

你可能感兴趣的:(python库)