0%

Python数据科学_29_时间数据处理_calendar库

calendar库

calendar的中文意思是“日历”,所以它其实适合进行日期,尤其是以日历的形式展示。

显示全年日历

calendar(theyear, w=2, l=1, c=6, m=3)

  • w:每日宽度间隔
  • l:每星期行数
  • c:每月间隔距离
  • m:每行显示的月份数

1_202303092128

1
2
3
# 查看2022年的日历,使用默认参数
calendar1 = calendar.calendar(2022)
print(calendar1)
                                  2022

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

1
2
3
# 改变参数
calendar2 = calendar.calendar(2022, w=3, l=1, c=8)
print(calendar2)
                                               2022

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

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

1
2
3
# 改变参数
calendar2 = calendar.calendar(2022, w=3, l=1, c=8, m=4)
print(calendar2)
                                                                2022

          January                            February                            March                              April
Mon Tue Wed Thu Fri Sat Sun        Mon Tue Wed Thu Fri Sat Sun        Mon Tue Wed Thu Fri Sat Sun        Mon Tue Wed Thu Fri Sat Sun
                      1   2              1   2   3   4   5   6              1   2   3   4   5   6                          1   2   3
  3   4   5   6   7   8   9          7   8   9  10  11  12  13          7   8   9  10  11  12  13          4   5   6   7   8   9  10
 10  11  12  13  14  15  16         14  15  16  17  18  19  20         14  15  16  17  18  19  20         11  12  13  14  15  16  17
 17  18  19  20  21  22  23         21  22  23  24  25  26  27         21  22  23  24  25  26  27         18  19  20  21  22  23  24
 24  25  26  27  28  29  30         28                                 28  29  30  31                     25  26  27  28  29  30
 31

            May                                June                               July                              August
Mon Tue Wed Thu Fri Sat Sun        Mon Tue Wed Thu Fri Sat Sun        Mon Tue Wed Thu Fri Sat Sun        Mon Tue Wed Thu Fri Sat Sun
                          1                  1   2   3   4   5                          1   2   3          1   2   3   4   5   6   7
  2   3   4   5   6   7   8          6   7   8   9  10  11  12          4   5   6   7   8   9  10          8   9  10  11  12  13  14
  9  10  11  12  13  14  15         13  14  15  16  17  18  19         11  12  13  14  15  16  17         15  16  17  18  19  20  21
 16  17  18  19  20  21  22         20  21  22  23  24  25  26         18  19  20  21  22  23  24         22  23  24  25  26  27  28
 23  24  25  26  27  28  29         27  28  29  30                     25  26  27  28  29  30  31         29  30  31
 30  31

         September                           October                            November                           December
Mon Tue Wed Thu Fri Sat Sun        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                      1   2   3   4
  5   6   7   8   9  10  11          3   4   5   6   7   8   9          7   8   9  10  11  12  13          5   6   7   8   9  10  11
 12  13  14  15  16  17  18         10  11  12  13  14  15  16         14  15  16  17  18  19  20         12  13  14  15  16  17  18
 19  20  21  22  23  24  25         17  18  19  20  21  22  23         21  22  23  24  25  26  27         19  20  21  22  23  24  25
 26  27  28  29  30                 24  25  26  27  28  29  30         28  29  30                         26  27  28  29  30  31
                                    31

显示某个月份日历

2_202303092128

1
print(calendar.month(2022, 11, w=2, l=1))
   November 2022
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

1
print(calendar.month(2022, 11, w=3, l=1))
       November 2022
Mon Tue Wed Thu Fri Sat Sun
      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

1
print(calendar.month(2022, 11, w=3, l=2))
       November 2022

Mon Tue Wed Thu Fri Sat Sun

      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


判断某个年份是不是闰年

1
print(calendar.isleap(2000))
True
1
print(calendar.isleap(2012))
True
1
print(calendar.isleap(2015))
False

判断两个年份之间有多少个闰年

1
calendar.leapdays(2000, 2020)  # 左闭右开
5
1
calendar.leapdays(2000, 2021)
6

通过列表的形式返回某年某月的日历

其中本月没有的日期用0表示

1
print(calendar.month(2022, 11))
   November 2022
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

1
calendar.monthcalendar(2022, 11)
[[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]]

返回某年某月从星期几开始,总共有多少天

1
calendar.monthrange(2022, 11)
(1, 30)

具体日期的星期数

1
calendar.weekday(2022, 11, 18)  # 0代表星期一,6代表星期天
4

返回星期数对于的缩写名称

1
calendar.weekheader(3)  # 3表示显示的英文长度为3
'Mon Tue Wed Thu Fri Sat Sun'
1
calendar.weekheader(9)
'  Monday   Tuesday  Wednesday  Thursday   Friday   Saturday   Sunday '
1
2
# 查看某天对应的星期英文缩写
calendar.weekheader(9).split()[calendar.weekday(2022, 11, 9)]
'Wednesday'

将时间元组转化为时间戳

1
2
print(time.time())  # 查看当前时间戳
print(calendar.timegm(time.gmtime())) # 将元组格式数据转化为时间戳
1669108716.0516217
1669108716

查看当前系统中星期和月份的表示

1
list(calendar.day_name)  # 完整写法
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
1
list(calendar.day_abbr)  # 缩写
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
1
list(calendar.month_name)  # 月份完整写法,第一个位置为空字符串
['',
 'January',
 'February',
 'March',
 'April',
 'May',
 'June',
 'July',
 'August',
 'September',
 'October',
 'November',
 'December']
1
list(calendar.month_abbr)
['',
 'Jan',
 'Feb',
 'Mar',
 'Apr',
 'May',
 'Jun',
 'Jul',
 'Aug',
 'Sep',
 'Oct',
 'Nov',
 'Dec']
-------------本文结束感谢您的阅读-------------