How to set the first day of each week to weekday using Python?


In this article, we will show you how to set the first day of each week to a weekday using Python.

Now we see 2 methods to accomplish this task−

  • Using setfirstweekday() function

  • Using setfirstweekday() function to set a given weekday Number

Method 1: Using setfirstweekday() function to set given weekDay

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the calendar module.

  • Use the prmonth() function (It is used to print the calendar for a month) of the calendar module to print the calendar for the given month and year.

  • Pass the day on which you want it to be the first Weekday as an argument to the calendar.setfirstweekday() function.

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.

Syntax

setfirstweekday()
Parameters - Doesn’t takes any parameters
Return Value - None
  • Check whether the weekday is modified or not by printing the firstweekday() function to get the value of the first weekday.

  • Print the calendar after setting the first day of the week as Sunday using the prmonth() function(It is used to print the calendar for a month) for the given month and year passed as arguments.

Example

The following program returns the calendar after setting the 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)

Output

On executing, the above program will generate the following output −

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

We first passed the year and month as arguments to the pr month function, which printed the calendar for the given year and month, and then we used the setfirstweekday() function to set the firstWeekday by passing the weekday we wanted, say Sunday, as an argument. Then we checked whether or not the firstWeekday had been updated by printing the value of firstWeekday() and the modified calendar.

Method 2: Using setfirstweekday() function to set given weekDay Number

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the calendar module.

  • Print the calendar for the given month and year by passing year, month, width, and lines as arguments to the prmonth() function(It is used to print the calendar for a month)

  • Pass the day number on which you want it to be the first Weekday as an argument to the calendar.setfirstweekday() function(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).

  • Check whether the weekday is modified or not by printing the firstweekday() function to get the value of the first weekday.

  • Print the calendar again after setting firstweekday.

Example

The following program returns the calendar after setting the 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)

Output

On executing, the above program will generate the following output −

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

Here, we use the setfirstweekday() function to set the firstWeekday by passing the weekday number we want, say 3, as an argument.

Conclusion

Using two different examples, we learned how to set the first weekday in this article. We also learned how to use the prmonth() function to print the calendar for the given month and year, as well as how to change its width and lines. We also learned how to use the firstWeekday() function to obtain the firstWeekday.

Updated on: 02-Nov-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements