Python 日历教程及示例

日历模块 Python 具有日历类,允许根据日期、月份和年份计算各种任务。除此之外,还有 TextCalendar 和 HTMLCalendar 类 Python 允许您编辑日历并根据您的要求使用。

让我们看看我们能做什么 Python 日历。

步骤 1) 运行代码。

日历 Python

  • 代码行# 1:我们从“导入日历”开始,它将导入该模块的所有类。
  • 代码行 # 3:c= calendar.TextCalendar(calendar.SUNDAY) 告诉解释器创建一个文本日历。月份的开始将是星期日。在 Python,您可以格式化日历,因为您可以更改月份的开始日期
  • 代码行 # 4:str= c.formatmonth(2025,1) 我们正在创建 2025 年 1 月(XNUMX 月)的日历
  • 代码行#5:print str 将打印输出。

让我们快速将值从星期日更改为星期四并检查输出

日历 Python

步骤2) 您还可以以 HTML 格式打印日历,如果开发人员想要更改日历的外观和感觉,此功能将非常有用

日历 Python

步骤3) 通过使用 c.itermonthday (2025,4),它将获取该月的总天数。

日历 Python

  • 当您执行代码来获取特定月份(比如“四月”)的总天数时,您将在输出中获得 30 天,但您也会在开头看到一些零,有时在结尾看到一些零。
  • 输出中的零表示星期几位于重叠的月份,这意味着它不属于该月份。
  • 这些零出现在输出中,是因为在你的代码中你提到了天(星期四),所以当你调用函数“c.itermonthdays”时,它会从星期四开始计算天数,而你的星期四可能不是从日期 1 开始的st 四月可能是28th 或29th 28 月,所以当你执行代码时,它会从 XNUMX 开始计算天数th 1 月及之后的任何日子,直到 XNUMXst 四月。这些天数将计为零,在输出中您将看到这些零,并且同样适用于月底。
  • 因此,除了日期 1-30 之外,所有前一个月以及后一个月的日期都将在输出中显示为零。

步骤4) 您可以从本地系统获取数据,例如月份或工作日等

日历 Python

  • 此处的输出显示我们已经从本地系统打印出了月份名称。同样,您也可以获取星期几的名称,如下所示
  • 输出将取决于本地系统,假设您的本地系统是其他国家/地区,那么它将根据该国/地区的本地设置提供输出。这里我们有几个月,所以不会有差异,但如果是一周或一天,肯定会有所不同。

日历 Python

步骤5) 您可以获取全年特定日期的列表。例如,每年的第一个月都有一个审计日 Monday 一周。你想知道第一次 Monday 每个月。您可以使用此代码

日历 Python

  • mycal = calendar.monthcalendar(2025, month) 将创建该月的日历
  • 将变量 week1 和 week2 设置为日历的第一周和第二周
  • 检查第 1 周是否包含 Monday,设定审计日期
  • 否则将审计日设为第一天 Monday 第二周
  • 输出显示第一个 Monday 属于该月。
  • 这个 Cal 对象的长度将根据月份有多少周而确定。在我们的例子中,它将是一或两周,因为首先 Monday 一周的第一天通常在第一周,如果不是,那么就考虑第二周。让我们详细看看为什么还要考虑第二周。
  • 这里我们使用日历的常数 Monday日历对象会给出代表星期日的常量, Monday、星期二等等。我们之前见过这些。因此,如果在第一周中 Monday 常数不等于 0,记住零表示属于另一个月的天数。因此,在这种情况下,如果它是零,它将是一个 Monday 属于上个月。但如果第一个 Monday 不等于 0,这意味着我的审计日将在第一周内。否则,如果为 0,则第一个 Monday 不是在该月的第一周,而是在第二周。
  • 所以我说好的,将我的审计日变量设置为 Monday 以第二周为代表。因此,审计日将以第一周或第二周的日期返回。

以下是完整的代码

Python 2示例

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示例

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

结语

  • In Python,您可以按照自己想要的方式格式化日历,因为您可以更改月份的开始日期
  • 以 HTML 格式打印日历
  • 从本地系统获取数据,例如月份或工作日
  • 获取全年特定日期的列表