Python中的Calendar模块具有Calendar类,该类允许基于日期,月份和年份来计算各种任务。 最重要的是,Python中的TextCalendar和HTMLCalendar类允许您编辑日历并根据需要使用。
让我们看看如何使用Python日历。
步骤1)运行代码。
- 代码行1:我们从“导入日历”开始,它将导入此模块的所有类。
- 代码行3:c = calendar.TextCalendar(calendar.SUNDAY)告诉解释器创建文本日历。 这个月的开始是星期日。 在Python中,您可以设置日历的格式,因为您可以更改月份的开始日期
- 代码行4:str = c.formatmonth(2025,1)我们正在创建2025年的日历,即月1 – 1月
- 第5行代码:print str将打印输出。
让我们快速将值从周日更改为周四并检查输出
第2步)您还可以将HTML格式的日历打印出来,如果开发人员希望对日历的外观和风格进行任何更改,此函数对开发人员很有帮助
步骤3)使用c.itermonthday(2025,4)在一个月的天中循环,它将获取该月的总天数。
- 当您执行代码以获取特定月份的总天数时,说出“ April”,那么您将获得30天的输出,但是在开始的时候(有时是在结尾),您也会看到一些零。 。
- 输出中的零表示一周中的某天是一个重叠的月份,这意味着它不属于该月份。
- 这些零出现在输出中是因为在您的代码中提到了天(星期四),因此,当您调用函数“ c.itermonthdays”时,它将开始计算从星期四开始的天数,而您的星期四可能不是从4月1日开始的3月28日或3月29日,因此当您执行代码时,它将开始计算从3月28日开始的天数,以及从3月28日到4月1日的任何天数。这些天将被计为零,并且在输出中您将看到这些零,并且对月末也是如此。
- 因此,除了日期1-30外,上个月和下个月的所有日期将在输出中显示为零。
步骤4)您可以从本地系统中获取数据,例如月份或工作日等
- 此处的输出显示我们已经从本地系统打印出月份名称。 同样,您也可以获取工作日名称,如下所示
- 输出将取决于本地系统,假设您的本地系统是其他国家/地区,则它将根据该国家/地区的本地设置给出输出。 在这里,我们有几个月的时间,所以不会有什么区别,但是如果是一周或一天,则肯定会有所不同。
步骤5)您可以获取全年特定日期的列表。 例如,一周的每个第一个星期一都有一个审核日。 您想知道每个月的第一个星期一的日期。 您可以使用此代码
- mycal = calendar.monthcalendar(2025,month)将创建该月份的日历
- 将变量week1和week2设置为日历的第一周和第二周
- 检查第1周是否包含星期一,设置审核日
- 否则将审核日设置为第2周的第一个星期一
- 输出显示该月的第一个星期一的日期。
- 该Cal对象的长度将确定为一定长度,具体取决于一个月中有多少周。在我们的情况下,它将是一两个,因为一周的第一个星期一通常会在第一周,但如果没有,则考虑第二周。让我们详细了解为什么我们还要考虑第二周。
- 在这里,我们使用日历的常量星期一,日历对象为您提供代表星期日,星期一,星期二等的常量。我们以前已经看过这些。因此,如果在一周的第一天,星期一常量表示的天不等于0,请记住零表示属于另一个月的天。因此,在这种情况下,如果它为零,那么它将是属于上个月的星期一。但是,如果第一个星期一不等于0,则意味着我的审核日将在一周之内。否则,如果该值为0,则第一个星期一不在该月的第一个星期,而必须在第二个星期。
- 因此,然后我说好,将我的审核日变量设置为第二周代表的星期一。因此,无论是第一周还是第二周,审计日都会回来。
这是完整的代码
Python 2 Example
import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
str = c.formatmonth(2025, 1, 0, 0)
print str
# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print str
# loop over the days of a month
# zeroes indicate that the day of the week is in a next month or overlapping month
for i in c.itermonthdays(2025, 4):
print i
# The calendar can give info based on local such a names of days and months (full and abbreviated forms)
for name in calendar.month_name:
print name
for day in calendar.day_name:
print day
# calculate days based on a rule: For instance an audit day on the second Monday of every month
# Figure out what days that would be for each month, we can use the script as shown here
for month in range(1, 13):
# It retrieves a list of weeks that represent the month
mycal = calendar.monthcalendar(2025, month)
# The first MONDAY has to be within the first two weeks
week1 = mycal[0]
week2 = mycal[1]
if week1[calendar.MONDAY] != 0:
auditday = week1[calendar.MONDAY]
else:
# if the first MONDAY isn't in the first week, it must be in the second week
auditday = week2[calendar.MONDAY]
print "%10s %2d" % (calendar.month_name[month], auditday)
Python 3 Example
import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
str = c.formatmonth(2025, 1, 0, 0)
print(str)
# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print(str)
# loop over the days of a month
# zeroes indicate that the day of the week is in a next month or overlapping month
for i in c.itermonthdays(2025, 4):
print(i)
# The calendar can give info based on local such a names of days and months (full and abbreviated forms)
for name in calendar.month_name:
print(name)
for day in calendar.day_name:
print(day)
# calculate days based on a rule: For instance an audit day on the second Monday of every month
# Figure out what days that would be for each month, we can use the script as shown here
for month in range(1, 13):
# It retrieves a list of weeks that represent the month
mycal = calendar.monthcalendar(2025, month)
# The first MONDAY has to be within the first two weeks
week1 = mycal[0]
week2 = mycal[1]
if week1[calendar.MONDAY] != 0:
auditday = week1[calendar.MONDAY]
else:
# if the first MONDAY isn't in the first week, it must be in the second week
auditday = week2[calendar.MONDAY]
print("%10s %2d" % (calendar.month_name[month], auditday))
摘要:
- 在Python中,可以更改日历的格式,因为您可以更改开始的月份日期
- 以HTML格式打印出日历
- 从本地系统中获取数据,例如几个月或工作日
- 获取全年特定日期的列表