提供与日历相关的有用功能
>>>import calendar
>>>calendar.setfirstweekday(firstweekday=6)
>>>import calendar
>>>calendar.setfirstweekday(firstweekday=6)
>>>print(calendar.firstweekday())
6
>>>import calendar
>>>print(calendar.isleap(2019))
>False
>>>import calendar
>>>print(calendar.leapdays(2008, 2019))
3
>>>import calendar
>>>print(calendar.weekday(2019, 4, 8))
0
>>>import calendar
>>>print(calendar.weekheader(3))
Sun Mon Tue Wed Thu Fri Sat
>>>import calendar
>>>print(calendar.monthrange(2019,4))
(0, 30)
>>>import calendar
>>>calendar.setfirstweekday(firstweekday=6)
>>>print(calendar.monthcalendar(2019, 4))
[[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来补充后面的
Calendar子类,firstweekday为一个整数,指定一周的第一天,0是星期一(默认),6为星期日
from calendar import TextCalendar
>>>from calendar import TextCalendar
>>>c = TextCalendar(firstweekday=6)
>>>print(c.formatmonth(2019, 4))
April 2019
Su Mo Tu We Th Fr Sa
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
>>>from calendar import TextCalendar
>>>a = TextCalendar(firstweekday=6)
>>>a.prmonth(2019, 4)
April 2019
Su Mo Tu We Th Fr Sa
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
>>>from calendar import TextCalendar
>>>c = TextCalendar(firstweekday=6)
>>>print(c.format(2019, m=4))
Calendar的子类,firstweekday为一个整数,指定一周的第一天,0是星期一(默认),6为星期日
>>>from calendar import HTMLCalendar
>>>c = HTMLCalendar(firstweekday=6)
>>>print(c.formatyear(2019, width=4))
>>>from calendar import HTMLCalendar
>>>c = HTMLCalendar(firstweekday=6)
>>>print(c.formatyearpage(2019, width=4))
上面就是calendar模块的常用函数,可以去python官网详细了解