Python 日历模块 calendar

____tz_zs

日历模块 calendar

calendar 模块中,周一到周日用 0 到 6 表示。
源码如下所示

(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)

calendar.calendar

calendar.calendar()
以一个多行字符串格式返回指定的年的日历
参数

  • w = 每个日期之间的间隔字符数
  • l = 每周所占用的行数
  • c = 每个月之间的间隔字符数

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import calendar

cal = calendar.calendar(theyear=2018, w=2, l=1, c=6, m=3)
print(type(cal))
print(cal)
"""

                                  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
"""

calendar.month

calendar.month() 获取某年某月的月日历字符串
格式:calendar.month(年,月)
返回值:月日历的字符串

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import calendar

m3 = calendar.month(theyear=2018, themonth=3)
print(m3)
"""
     March 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.monthcalendar

calendar.monthcalendar() 返回某年某月的月日历矩阵,每行代表一周
格式:calendar.monthcalendar(年,月)
返回值:矩阵
注意:矩阵中没有天数用0表示

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import calendar

m = calendar.monthcalendar(year=2018, month=3)
print(type(m))
print(m)
"""

[[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]]
"""

calendar.monthrange

calendar.monthrange() 返回指定月份第一天(即1号)的星期日期,和本月的总天数
格式:calendar.monthrange(年,月)
回值:元组(int 1号是周几,int 总天数)
注意:日历一周默认为 0 -6 表示周一到周天

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import calendar

w, t = calendar.monthrange(year=2019, month=1)
print(w)
print(t)
"""
1
31
"""

应用
获取指定月的一点和最后一天

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import calendar
from datetime import datetime
from datetime import date

w, t = calendar.monthrange(year=2019, month=1)
# 获取指定月的一点和最后一天
firstDay = datetime(year=2019, month=1, day=1)  # 
lastDay = datetime(year=2019, month=1, day=t)
print(firstDay)
print(lastDay)
"""
2019-01-01 00:00:00
2019-01-31 00:00:00
"""
firstDay = date(year=2019, month=1, day=1)  # 
lastDay = date(year=2019, month=1, day=t)
print(firstDay)
print(lastDay)
"""
2019-01-01
2019-01-31
"""

calendar.weekday

calendar.weekday() 返回指定日期所对应的星期日期
格式:calendar.weekday(年,月,日)
返回值:0(周一)~ 6(周日)

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import calendar

w = calendar.weekday(year=2019, month=1, day=24)
print(w)
"""
3
"""

你可能感兴趣的:(#,综合)