亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 日历示例教程

Python 中的日历模块具有日历类,允许基于日期、月份和年份计算各种任务。最重要的是,Python 中的 TextCalendar 和 HTMLCalendar 类允许您编辑日历并根据您的要求使用。

让我们看看我们可以用 Python 日历做什么。

步骤1) 运行代码。

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

步骤 2) 您还可以打印 HTML 格式的日历,如果开发人员想对日历的外观和感觉进行任何更改,此功能将很有帮助

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

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

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

这是完整的代码

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

总结:


Python

  1. C# 抽象类教程与示例:什么是抽象?
  2. C# Windows 窗体应用程序示例教程
  3. Python String strip() 函数与示例
  4. 带有示例的 Python 字符串计数()
  5. 带有示例的 Python round() 函数
  6. 带有示例的 Python map() 函数
  7. Python Timeit() 与示例
  8. Python 教程中的收益:生成器和收益与返回示例
  9. 集合中的 Python 计数器示例
  10. 带有示例的 Python 列表计数()
  11. Python 列表 index() 与示例
  12. Python - 使用 C 进行扩展编程