calendar:用于处理日期和日历的模块。
calendar模块提供了一系列函数和类用于处理日历相关的操作。以下是该模块中一些常用的函数和方法:
一、常用方法
(一)函数
calendar.calendar(year, w=2, l=1, c=6, m=3): 返回一个多行字符串格式的年历。
calendar.month(year, month, w=2, l=1): 返回一个多行字符串格式的月历。
calendar.isleap(year): 判断某年是否为闰年。
calendar.leapdays(y1, y2): 返回在y1和y2之间的闰年数量(不包含y2)。
calendar.monthrange(year, month): 返回一个元组,第一个元素是该月的第一天是星期几(0-6),第二个元素是该月有多少天。
calendar.monthcalendar(year, month): 返回一个整型矩阵,表示一个月的日历。
calendar.weekday(year, month, day): 返回给定日期是星期几(0-6)。
calendar.weekheader(n): 返回一个宽度为n的周几名称头。
(二)类
calendar.Calendar(firstweekday=0): 创建一个以firstweekday为一周开始的Calendar对象,默认是星期一。
calendar.TextCalendar(firstweekday=0): 生成文本格式的日历。
calendar.HTMLCalendar(firstweekday=0): 生成HTML格式的日历。
calendar.LocaleTextCalendar(firstweekday=0, locale=None): 生成本地化文本格式的日历。
calendar.LocaleHTMLCalendar(firstweekday=0, locale=None): 生成本地化HTML格式的日历。
(三)方法(以TextCalendar为例)
formatyear(theyear, w=2, l=1, c=6, m=3): 返回指定年份的文本格式年历。
formatmonth(theyear, themonth, w=2, l=1): 返回指定月份的文本格式月历。
iterweekdays(): 返回一周中每一天的序号,从firstweekday开始。
itermonthdates(year, month): 返回一个迭代器,生成指定月份的每一天的日期对象。
itermonthdays(year, month): 返回一个迭代器,生成指定月份的每一天的日期数字。
一、使用案例(函数)
import calendar
# calendar.calendar(year, w=2, l=1, c=6, m=3)
year_calendar = calendar.calendar(2023, w=2, l=1, c=6, m=3)
print("Year 2023 Calendar:\n", year_calendar)
# calendar.month(year, month, w=2, l=1)
october_calendar = calendar.month(2023, 10, w=2, l=1)
print("October 2023 Calendar:\n", october_calendar)
# calendar.isleap(year)
is_leap = calendar.isleap(2024)
print("Is 2024 a leap year?", is_leap)
# calendar.leapdays(y1, y2)
leap_years = calendar.leapdays(2000, 2023)
print("Leap years between 2000 and 2023:", leap_years)
# calendar.monthrange(year, month)
first_day, num_days = calendar.monthrange(2023, 10)
print("October 2023 starts on day:", first_day, "and has", num_days, "days")
# calendar.monthcalendar(year, month)
month_cal_matrix = calendar.monthcalendar(2023, 10)
print("October 2023 month calendar matrix:\n", month_cal_matrix)
# calendar.weekday(year, month, day)
weekday = calendar.weekday(2023, 10, 1)
print("October 1, 2023 is a weekday:", weekday)
# calendar.weekheader(n)
week_header = calendar.weekheader(3)
print("Week header with width 3:", week_header)
输出结果
Year 2023 Calendar:
2023
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 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
30 31
。。。。。。。(内容太多了删了些)
October 2023 Calendar:
October 2023
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
Is 2024 a leap year? True
Leap years between 2000 and 2023: 6
October 2023 starts on day: 6 and has 31 days
October 2023 month calendar matrix:
[[0, 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, 31, 0, 0, 0, 0, 0]]
October 1, 2023 is a weekday: 6
Week header with width 3: Mon Tue Wed Thu Fri Sat Sun
三、使用案例(类)
import calendar
# 创建一个Calendar对象,以星期天为一周的开始
cal = calendar.Calendar(firstweekday=6)
print("Days in October 2023 using Calendar:")
for day in cal.itermonthdates(2023, 10):
print(day, end=' ')
print("\n")
# 使用TextCalendar生成文本格式的月历
text_cal = calendar.TextCalendar(firstweekday=6)
formatted_month = text_cal.formatmonth(2023, 10)
print("Text Calendar for October 2023:\n", formatted_month)
# 使用HTMLCalendar生成HTML格式的月历
html_cal = calendar.HTMLCalendar(firstweekday=6)
formatted_html_month = html_cal.formatmonth(2023, 10)
print("HTML Calendar for October 2023:\n", formatted_html_month)
# 使用LocaleTextCalendar生成本地化文本格式的月历
locale_text_cal = calendar.LocaleTextCalendar(firstweekday=6, locale='en_US')
formatted_locale_month = locale_text_cal.formatmonth(2023, 10)
print("Locale Text Calendar for October 2023:\n", formatted_locale_month)
输出结果
Days in October 2023 using Calendar:
2023-10-01 2023-10-02 2023-10-03 2023-10-04 2023-10-05 2023-10-06 2023-10-07 2023-10-08 2023-10-09 2023-10-10 2023-10-11 2023-10-12 2023-10-13 2023-10-14 2023-10-15 2023-10-16 2023-10-17 2023-10-18 2023-10-19 2023-10-20 2023-10-21 2023-10-22 2023-10-23 2023-10-24 2023-10-25 2023-10-26 2023-10-27 2023-10-28 2023-10-29 2023-10-30 2023-10-31 2023-11-01 2023-11-02 2023-11-03 2023-11-04
Text Calendar for October 2023:
October 2023
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 31
HTML Calendar for October 2023:
<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">October 2023</th></tr>
<tr><th class="sun">Sun</th><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></tr>
<tr><td class="sun">1</td><td class="mon">2</td><td class="tue">3</td><td class="wed">4</td><td class="thu">5</td><td class="fri">6</td><td class="sat">7</td></tr>
<tr><td class="sun">8</td><td class="mon">9</td><td class="tue">10</td><td class="wed">11</td><td class="thu">12</td><td class="fri">13</td><td class="sat">14</td></tr>
<tr><td class="sun">15</td><td class="mon">16</td><td class="tue">17</td><td class="wed">18</td><td class="thu">19</td><td class="fri">20</td><td class="sat">21</td></tr>
<tr><td class="sun">22</td><td class="mon">23</td><td class="tue">24</td><td class="wed">25</td><td class="thu">26</td><td class="fri">27</td><td class="sat">28</td></tr>
<tr><td class="sun">29</td><td class="mon">30</td><td class="tue">31</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr>
</table>
四、使用案例(以TextCalendar为例)
import calendar
# 使用 TextCalendar 创建一个以星期天为一周开始的日历
text_cal = calendar.TextCalendar(firstweekday=6)
# formatyear(theyear, w=2, l=1, c=6, m=3)
formatted_year = text_cal.formatyear(2023, w=2, l=1, c=6, m=3)
print("Formatted Year Calendar for 2023:\n", formatted_year)
# formatmonth(theyear, themonth, w=2, l=1)
formatted_month = text_cal.formatmonth(2023, 10, w=2, l=1)
print("Formatted Month Calendar for October 2023:\n", formatted_month)
# iterweekdays(): 返回一周中每一天的序号,从firstweekday开始
weekdays = list(text_cal.iterweekdays())
print("Weekdays starting from Sunday:", weekdays)
# itermonthdates(year, month): 返回一个迭代器,生成指定月份的每一天的日期对象
month_dates = list(text_cal.itermonthdates(2023, 10))
print("Dates in October 2023:")
print(month_dates)
# itermonthdays(year, month): 返回一个迭代器,生成指定月份的每一天的日期数字
month_days = list(text_cal.itermonthdays(2023, 10))
print("Day numbers in October 2023:")
print(month_days)
输出结果
这些示例展示了如何使用calendar.TextCalendar类的方法来生成和格式化日历信息:
formatyear:生成指定年份的文本格式年历。
formatmonth:生成指定月份的文本格式月历。
iterweekdays:返回一周中每一天的序号,从指定的firstweekday开始。
itermonthdates:生成指定月份的每一天的日期对象。
itermonthdays:生成指定月份的每一天的日期数字。
Formatted Year Calendar for 2023:
2023
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
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
。。。。。。。。。(内容太多这里删了)
Formatted Month Calendar for October 2023:
October 2023
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 31
Weekdays starting from Sunday: [6, 0, 1, 2, 3, 4, 5]
Dates in October 2023:
[datetime.date(2023, 10, 1), datetime.date(2023, 10, 2), datetime.date(2023, 10, 3), datetime.date(2023, 10, 4), datetime.date(2023, 10, 5), datetime.date(2023, 10, 6), datetime.date(2023, 10, 7), datetime.date(2023, 10, 8), datetime.date(2023, 10, 9), datetime.date(2023, 10, 10), datetime.date(2023, 10, 11), datetime.date(2023, 10, 12), datetime.date(2023, 10, 13), datetime.date(2023, 10, 14), datetime.date(2023, 10, 15), datetime.date(2023, 10, 16), datetime.date(2023, 10, 17), datetime.date(2023, 10, 18), datetime.date(2023, 10, 19), datetime.date(2023, 10, 20), datetime.date(2023, 10, 21), datetime.date(2023, 10, 22), datetime.date(2023, 10, 23), datetime.date(2023, 10, 24), datetime.date(2023, 10, 25), datetime.date(2023, 10, 26), datetime.date(2023, 10, 27), datetime.date(2023, 10, 28), datetime.date(2023, 10, 29), datetime.date(2023, 10, 30), datetime.date(2023, 10, 31), datetime.date(2023, 11, 1), datetime.date(2023, 11, 2), datetime.date(2023, 11, 3), datetime.date(2023, 11, 4)]
Day numbers in October 2023:
[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, 0, 0, 0]