如何使用 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。