一 前言
calendar是和日历模块,calendar模块文件里定义了很多类型,主要有Calendar,TextCalendar以及HTMLCalendar类型。其中,Calendar是TextCalendar与HTMLCalendar的基类。
公众号:知识追寻者
知识追寻者(Inheriting the spirit of open source, Spreading technology knowledge;)
二 calendar
2.1firstweekday()
firstweekday(): 返回当前每周起始日期值。默认情况下,首次载入calendar模块时返回0,即星期一
import calendar
# 0
print(calendar.firstweekday())
2.2 setfirstweekday()
setfirstweekday(firstweekday) :通过常量 MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY、SATURDAY 和 SUNDAY 设置星期;其中 0 表示星期一,以此类推6表示星期日;
import calendar
calendar.setfirstweekday(calendar.SUNDAY)
# 6
print(calendar.firstweekday())
2.3 isleap()
isleap(year):判断是否是闰年,闰年为True,平年为False
import calendar
# False
print(calendar.isleap(2018))
# True
print(calendar.isleap(2016))
2.4 leapdays()
leapdays(y1, y2): 返回[y1,y2)年份之间的闰年数量;
import calendar
# 2
print(calendar.leapdays(2012, 2020))
2.5weekday
weekday(year, month, day):获取指定日期为星期几
import calendar
# 0 表示星期一
print(calendar.weekday(2020,5,11))
2.6 monthrange()
monthrange(year, month): 返回元组, 第一个值代表给定月份第一天的星期;第二个值代表给定月份有多少天;
import calendar
# (5, 29) -- 表示周六,有29天
print(calendar.monthrange(2020, 2))
# (4, 31) -- 表示周五,有31天
print(calendar.monthrange(2020, 5))
2.7 weekheader()
weekheader(n):返回包含星期的英文缩写,n表示返回缩写的长度;
import calendar
# Sun Mon Tue Wed Thu Fri Sat
print(calendar.weekheader(4))
2.8 monthcalendar()
monthcalendar(year, month): 返回表示一个月的日历的矩阵,每一行代表一周; 可以通过setfirstweekday() 改变默认值;
import calendar
print(calendar.monthcalendar(2020, 5))
输出
[[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]]
2.9 prmonth()
prmonth(theyear, themonth, w=0, l=0): 打印一个月的日历
- theyear : 指定年份
- themonth:指定月份
- w:日之间的间隔w个字符,默认0;
- l : 星期之间的行数,默认0;
import calendar
print(calendar.prmonth(2020, 5))
输出
May 2020
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
None
2.10 prcal()
prcal(year, w=0, l=0, c=6, m=3) : 打印一年的日历
- year: 指定年份
- w:日之间的间隔w个字符,默认0;
- l : 星期之间的行数,默认0;
- c : 月之间的宽度;
- m: 将12个月分为m列
import calendar
calendar.prcal(2020, m=4)
输出
2020
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 1 2 1
6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
27 28 29 30 31 24 25 26 27 28 29 23 24 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 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 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 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 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 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
2.11 day_name
返回星期列表
import calendar
days = [day for day in calendar.day_name]
print(days)
输出
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
2.12 month_name
返回月份列表
import calendar
months = [month for month in calendar.month_name]
print(months)
输出
['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
三 Calendar类
Calendar
对象提供了一些可被用于准备日历数据格式化的方法。 这个类本身不执行任何格式化操作
3.1 iterweekdays()
获取一周的数字的迭代器
from calendar import Calendar
car = Calendar()
weeks = [ value for value in car.iterweekdays()]
print(weeks)
输出
[0, 1, 2, 3, 4, 5, 6]
3.2termonthdates()
termonthdates(year, month)获取一个月日期的迭代器
from calendar import Calendar
car = Calendar()
date = [date for date in car.itermonthdates(2020,5)]
print(date)
输出
[datetime.date(2020, 4, 27), datetime.date(2020, 4, 28), datetime.date(2020, 4, 29), datetime.date(2020, 4, 30), datetime.date(2020, 5, 1),..........]
3.3itermonthdays()
itermonthdays(year, month)返回一个月天数的迭代器
from calendar import Calendar
car = Calendar()
days = [day for day in car.itermonthdays(2020,5)]
print(days)
输出
[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]
四 TextCalendar 类
TextCalendar 对象提供了一些生成纯文本日历的方法;
4.1 formatmonth()
formatmonth(theyear, themonth, w=0, l=0) 返回一个月的日历 ; 其中 prmonth(theyear, themonth, w=0, l=0) 打印结果 与formatmonth 一样,之间打印,不用写打印函数;
theyear
指定年;themonth
指定月- w: 日期之间的宽度
- l : 指定每行日期之间的行数
from calendar import TextCalendar
textcar = TextCalendar()
print(textcar.formatmonth(2020, 5))
输出
May 2020
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
4.2 formatyear()
formatyear
(theyear, w=2, l=1, c=6, m=3) : 返回m 个列的日历;其中 pryear
(theyear, w=2, l=1, c=6, m=3) 与其一样;
2020
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 1 2 1
6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
27 28 29 30 31 24 25 26 27 28 29 23 24 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 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 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 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 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 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
五 HTMLCalendar 类
calendar.HTMLCalendar
(firstweekday=0)
可以使用HTMLCalendar 类生成 HTML 日历;其方法与之前的TextCalendar 大多类似,简单示例如下
from calendar import HTMLCalendar
htmlCar = HTMLCalendar()
print(htmlCar.formatmonth(2020, 5, withyear=False))
输出
<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">May</th></tr>
<tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr>
<tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="fri">1</td><td class="sat">2</td><td class="sun">3</td></tr>
<tr><td class="mon">4</td><td class="tue">5</td><td class="wed">6</td><td class="thu">7</td><td class="fri">8</td><td class="sat">9</td><td class="sun">10</td></tr>
<tr><td class="mon">11</td><td class="tue">12</td><td class="wed">13</td><td class="thu">14</td><td class="fri">15</td><td class="sat">16</td><td class="sun">17</td></tr>
<tr><td class="mon">18</td><td class="tue">19</td><td class="wed">20</td><td class="thu">21</td><td class="fri">22</td><td class="sat">23</td><td class="sun">24</td></tr>
<tr><td class="mon">25</td><td class="tue">26</td><td class="wed">27</td><td class="thu">28</td><td class="fri">29</td><td class="sat">30</td><td class="sun">31</td></tr>
</table>
六官方文档
https://docs.python.org/3/library/calendar.html
转载:https://blog.csdn.net/youku1327/article/details/106227420