The Python calendar.setfirstweekday() function sets the first day of the week for the calendar module.
By default, the first weekday is set to Monday (0), but it can be changed to any other day of the week.
SyntaxFollowing is the syntax of the Python calendar.setfirstweekday() function −
calendar.setfirstweekday(weekday)Parameters
This function accepts an integer (0-6) as a parameter representing the new first weekday.
Return ValueThis function does not return a value. It updates the first weekday setting for the calendar module.
Example: Setting First Weekday to SundayIn this example, we change the first weekday to Sunday using the calendar.setfirstweekday() function −
import calendar # Set first weekday to Sunday calendar.setfirstweekday(calendar.SUNDAY) # Verify the change print("Updated first weekday:", calendar.firstweekday())
Following is the output obtained −
Updated first weekday: 6Example: Setting First Weekday to Wednesday
We can also set the first weekday to any other day, like Wednesday using the calendar.setfirstweekday() function −
import calendar # Set first weekday to Wednesday calendar.setfirstweekday(calendar.WEDNESDAY) # Verify the change print("Updated first weekday:", calendar.firstweekday())
Following is the output of the above code −
Updated first weekday: 2Example: Setting First Weekday and Printing a Month
Changing the first weekday affects how the month calendar is displayed as shown in the example below −
import calendar # Set first weekday to Friday calendar.setfirstweekday(calendar.FRIDAY) # Print the month calendar for March 2025 print(calendar.month(2025, 3))
We get the output as shown below −
March 2025 Fr Sa Su Mo Tu We Th 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...Example: Resetting First Weekday to Default
To reset the first weekday back to Monday, we explicitly set it again as shown in the following example −
import calendar # Reset to default (Monday) calendar.setfirstweekday(calendar.MONDAY) # Verify the change print("Reset first weekday:", calendar.firstweekday())
The result produced is as follows −
Reset first weekday: 0
python_date_time.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4