✍55 calendar 日历模块简单使用

有趣的日历模块,记录一下

简单使用

  • 打印日历 : calendar.calendar( )

import calendar

cc = calendar.calendar(2021)
bb = calendar.calendar(2021,w=4,l=2,c=6)  # 后三个参数都是用来设置间距
print(type(cc))  # str 
print(cc)
print(bb)
"""
<class 'str'>
                                  2021

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

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

                                             2021



          January                          February                          March

Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun

                  1   2   3        1   2   3   4   5   6   7        1   2   3   4   5   6   7

  4   5   6   7   8   9  10        8   9  10  11  12  13  14        8   9  10  11  12  13  14

 11  12  13  14  15  16  17       15  16  17  18  19  20  21       15  16  17  18  19  20  21

 18  19  20  21  22  23  24       22  23  24  25  26  27  28       22  23  24  25  26  27  28

 25  26  27  28  29  30  31                                        29  30  31



           April                             May                              June

Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun

              1   2   3   4                            1   2            1   2   3   4   5   6

  5   6   7   8   9  10  11        3   4   5   6   7   8   9        7   8   9  10  11  12  13

 12  13  14  15  16  17  18       10  11  12  13  14  15  16       14  15  16  17  18  19  20

 19  20  21  22  23  24  25       17  18  19  20  21  22  23       21  22  23  24  25  26  27

 26  27  28  29  30               24  25  26  27  28  29  30       28  29  30

                                  31



            July                            August                         September

Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun

              1   2   3   4                                1                1   2   3   4   5

  5   6   7   8   9  10  11        2   3   4   5   6   7   8        6   7   8   9  10  11  12

 12  13  14  15  16  17  18        9  10  11  12  13  14  15       13  14  15  16  17  18  19

 19  20  21  22  23  24  25       16  17  18  19  20  21  22       20  21  22  23  24  25  26

 26  27  28  29  30  31           23  24  25  26  27  28  29       27  28  29  30

                                  30  31



          October                          November                         December

Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun

                  1   2   3        1   2   3   4   5   6   7                1   2   3   4   5

  4   5   6   7   8   9  10        8   9  10  11  12  13  14        6   7   8   9  10  11  12

 11  12  13  14  15  16  17       15  16  17  18  19  20  21       13  14  15  16  17  18  19

 18  19  20  21  22  23  24       22  23  24  25  26  27  28       20  21  22  23  24  25  26

 25  26  27  28  29  30  31       29  30                           27  28  29  30  31
"""
  • 只返回某个月的日历 : calendar.month( )

ee = calendar.month(2021,6,w=2,l=1)
print(ee)
"""
     June 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
"""
  • 判断是否是闰年 : calendar.isleap( )

dd = calendar.isleap(2016)
dd2 = calendar.isleap(2017)
print(dd)
print(dd2)
"""
True
False
"""
  • 返回某两年之间的闰年总数 : calendar.leapdays( )

gg = calendar.leapdays(2011,2022)  # 闭区间
print(gg)
"""
3
"""
  • 以列表套列表的形式某个月的日历 :calendar.monthcalendar()

ff = calendar.monthcalendar(2021,6)
print(ff)
print('-------->')
for i in ff:
    print(i)
"""
[[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, 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, 0, 0, 0, 0]
"""
  • 输入年月返回该月第一天和该月天数 :calendar.monthrange ( )

hh = calendar.monthrange(2021, 12)
print(hh)
"""
(2, 31)  # 12月的第一天是星期二,共有31天
"""
  • 将一个元组时间变成时间戳 :calendar.timegm()

import time

kk = calendar.timegm(time.localtime())
kk2 = calendar.timegm((2021,12,2,13,56,8))  # 时间元组必须包含年月日时分秒
kk3 = int(time.time())
print(kk)
print(kk2)
print(kk3)
"""
1638453562
1638453368
1638424762
"""
posted @ 2021-12-02 14:10  给你骨质唱疏松  阅读(118)  评论(0编辑  收藏  举报