Python 如何将每周的第一天设置为工作日
在本文中,我们将向您展示如何使用Python将每周的第一天设置为工作日。
现在我们将介绍两种完成此任务的方法:
- 使用setfirstweekday()函数
-
使用setfirstweekday()函数来设置给定的工作日数字
方法1:使用setfirstweekday()函数设置给定的工作日
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入calendar模块。
-
使用calendar模块的prmonth()函数(用于打印给定月份的日历)打印给定月份和年份的日历。
-
将您希望成为第一个工作日的日期作为参数传递给calendar.setfirstweekday()函数。
calendar.setfirstweekday(weekday)是Python的calendar模块中的一个函数,用于创建简单的文本日历。
setfirstweekday()方法设置每周开始的星期几(0表示周一,6表示周日)。为了方便使用,我们提供了值MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY、SATURDAY和SUNDAY。
语法
setfirstweekday()
Parameters - Doesn’t takes any parameters
Return Value - None
- 打印firstweekday()函数以检查工作日是否已修改,以获取第一个工作日的值。
-
将每周的第一天设置为Sunday后,使用 prmonth() 函数(用于打印给定月份和年份的日历)打印日历。
示例
以下程序在设置了第一个工作日后返回日历。
# 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
我们首先将年份和月份作为参数传递给prmonth函数,该函数打印给定年份和月份的日历,然后我们使用setfirstweekday()函数通过将我们想要的星期几(例如Sunday)作为参数来设置第一天的星期几。然后,我们通过打印firstweekday()函数的值和修改后的日历来检查第一天的星期几是否已更新。
方法2:使用setfirstweekday()函数设置给定的星期几
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入calendar模块。
-
通过将年份、月份、宽度和行数作为参数传递给prmonth()函数(用于打印一个月的日历),打印给定月份和年份的日历。
-
将你想要设为第一天星期几的日期作为参数传递给calendar.setfirstweekday()函数(setfirstweekday()方法设置星期几(0代表星期一,6代表星期日),以在每周开始的日期。为方便起见,提供了MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY、SATURDAY和SUNDAY的值)。
-
通过打印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,来设置第一个星期几。
结论
通过两个不同的示例,我们学会了如何设置第一个星期几。我们还学会了如何使用prmonth()函数来打印给定月份和年份的日历,以及如何更改它的宽度和行数。我们还学会了如何使用firstWeekday()函数来获取第一个星期几。
极客笔记