Python calendar.setfirstweekday() Function



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.

Syntax

Following 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 Value

This function does not return a value. It updates the first weekday setting for the calendar module.

Example: Setting First Weekday to Sunday

In 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: 6

Example: 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: 2

Example: 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
Advertisements