
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
- Related Articles
- How to get first day of the week using Python?
- How to set the first day of the week in sap.m.DatePicker's calendar?
- How to get first day of week in PHP?
- MySQL query to subtract date records with week day and display the weekday with records
- Java Program to get day of week for the last day of each month in 2019
- How to get beginning of the first day of this week from timestamp in Android sqlite?
- How to get the day of the week in JavaScript?
- How to get day of month, day of year and day of week in android using offset date time API class?
- Formatting day of week using SimpleDateFormat in Java
- Display Day Name of Week using Java Calendar
- C# Program to get current day of week
- Day of the Week in C++
- Get the week number in MySQL for day of week?
- Java Program to get the day of the week from GregorianCalendar
- How to return the day of the week in the specified date according to universal time?
