如何使用 Python 将每周的第一天设置为工作日?
在本文中,我们将向您展示如何使用 Python 将每周的第一天设置为工作日。
现在我们看到两种方法可以完成此任务:
使用 setfirstweekday() 函数
使用 setfirstweekday() 函数设置给定的工作日编号
方法 1:使用 setfirstweekday() 函数设置给定的工作日
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字,导入 calendar 模块。
使用 calendar 模块的 prmonth() 函数(用于打印一个月的日历)来打印给定月份和年份的日历。
将您希望作为第一工作日的日期作为参数传递给 calendar.setfirstweekday() 函数。
The calendar.setfirstweekday(weekday) is a function in Python's calendar module for creating simple text calendars. The setfirstweekday() method sets the weekday (0 is Monday, 6 is Sunday) on which each week starts. For your convenience, the values MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are provided.
语法
setfirstweekday() Parameters - Doesn’t takes any parameters Return Value - None
通过打印 firstweekday() 函数获取第一工作日的数值,检查工作日是否已修改。
使用 prmonth() 函数(用于打印一个月的日历)打印设置每周第一天为星期日后的日历,将给定的月份和年份作为参数传递。
示例
以下程序在设置 firstWeekday 后返回日历:
# importing calendar module import calendar # printing the calendar for the specified month and year calendar.prmonth(2017, 3) # setting the first day of the week as Sunday calendar.setfirstweekday(calendar.SUNDAY) # Printing new line for separation print () # using firstweekday() to check the changed day print("The new week day number is: ", end ="") print(calendar.firstweekday()) # printing the calendar after setting the first day of the week as the Sunday calendar.prmonth(2017, 3)
输出
执行上述程序将生成以下输出:
March 2017
Th Fr Sa Su Mo Tu We
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
The new week day number is: 6
March 2017
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
我们首先将年份和月份作为参数传递给 pr month 函数,该函数打印了给定年份和月份的日历,然后我们使用 setfirstweekday() 函数通过传递我们想要的星期日作为参数来设置 firstWeekday。然后,我们通过打印 firstWeekday() 的值和修改后的日历来检查 firstWeekday 是否已更新。
方法 2:使用 setfirstweekday() 函数设置给定的工作日编号
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字,导入 calendar 模块。
通过将年份、月份、宽度和行数作为参数传递给 prmonth() 函数(用于打印一个月的日历),打印给定月份和年份的日历。
将您希望作为第一工作日的日期编号作为参数传递给 calendar.setfirstweekday() 函数(setfirstweekday() 方法设置每个星期开始的工作日(0 为星期一,6 为星期日)。为了方便起见,提供了 MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY、SATURDAY 和 SUNDAY 的值)。
通过打印 firstweekday() 函数获取第一工作日的数值,检查工作日是否已修改。
设置 firstweekday 后再次打印日历。
示例
以下程序在设置 firstWeekday 后返回日历:
# importing calendar module import calendar # printing the calendar of the 6th month, of 2020 by passing year, month, width, and lines as arguments. print ("Calendar of 6th month, 2020:") calendar.prmonth(2020, 6, 2, 1) # setting the first week day number calendar.setfirstweekday(3) # Printing new line for separation print () # using firstweekday() to check the changed day print("The new week day number is : ", end ="") print(calendar.firstweekday()) # Printing calendar calendar.prmonth(2020, 6, 2, 1)
输出
执行上述程序将生成以下输出:
Calendar of 6th month, 2020:
June 2020
Th Fr Sa Su Mo Tu We
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
The new week day number is : 3
June 2020
Th Fr Sa Su Mo Tu We
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
这里,我们使用 setfirstweekday() 函数通过传递我们想要的星期数 3 作为参数来设置 firstWeekday。
结论
通过两个不同的示例,我们学习了如何在本文中设置第一个工作日。我们还学习了如何使用 prmonth() 函数打印给定月份和年份的日历,以及如何更改其宽度和行数。我们还学习了如何使用 firstWeekday() 函数获取 firstWeekday。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP