Python calendar.firstweekday() Function



The Python calendar.firstweekday() function is used to get the current setting for the first day of the week in the calendar module.

By default, the first weekday is set to Monday (0), but it can be changed using calendar.setfirstweekday() function.

Syntax

Following is the syntax of the Python calendar.firstweekday() function −

calendar.firstweekday()

Parameters

This function does not accept any parameters.

Return Value

This function returns an integer representing the first weekday. The mapping is −

  • 0 - Monday
  • 1 - Tuesday
  • 2 - Wednesday
  • 3 - Thursday
  • 4 - Friday
  • 5 - Saturday
  • 6 - Sunday

Example: Getting the Default First Weekday

By default, the first weekday is Monday (0) −

import calendar

# Get the default first weekday
first_day = calendar.firstweekday()

print("Default first weekday:", first_day)  

We get the output as shown below −

Default first weekday: 0

Example: Changing and Retrieving the First Weekday

We can use the calendar.setfirstweekday() function to change the first weekday before calling firstweekday() function −

import calendar

# Set first weekday to Sunday
calendar.setfirstweekday(calendar.SUNDAY)

# Get the updated first weekday
first_day = calendar.firstweekday()

print("Updated first weekday:", first_day)

Following is the output of the above code −

Updated first weekday: 6

Example: Using First Weekday in Month Calendar

Changing the first weekday affects how the month calendar is displayed −

import calendar

# Set first weekday to Wednesday
calendar.setfirstweekday(calendar.WEDNESDAY)

# Print the month calendar
print(calendar.month(2025, 3))

Following is the output obtained −

March 2025
We Th Fr Sa Su Mo Tu
          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

Example: Resetting to Default

To reset the first weekday back to Monday, we explicitly set it again −

import calendar

# Reset to default (Monday)
calendar.setfirstweekday(calendar.MONDAY)

# Verify the change
print("Reset first weekday:", calendar.firstweekday())  

The result produced is as shown below −

Reset first weekday: 0
python_date_time.htm
Advertisements