Python calendar.weekday() Function



The Python calendar.weekday() function is used to get the weekday of a specific date.

The function returns an integer representing the day of the week, where Monday is 0 and Sunday is 6.

Syntax

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

weekday = calendar.weekday(year, month, day)

Parameters

This function accepts the following parameters −

  • year: The year of the date.
  • month: The month of the date (1-12).
  • day: The day of the date (1-31).

Return Value

This function returns an integer representing the weekday:

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

Example: Weekday of a Specific Date

In this example, we find the weekday for March 14, 2025 using the calendar.weekday() function −

import calendar

weekday = calendar.weekday(2025, 3, 14)
print("Weekday:", weekday)

Following is the output obtained −

Weekday: 4  # (Friday)

Example: Weekday for Today's Date

We can use the datetime module to get today's date and find the weekday −

import calendar
import datetime

# Get today's date
today = datetime.date.today()

# Find the weekday
weekday = calendar.weekday(today.year, today.month, today.day)
print("Today's Weekday:", weekday)

Following is the output of the above code −

Today's Weekday: 2  # (Example output, varies based on the current date)

Example: Checking if a Given Date is a Weekend

We can determine if a date falls on a weekend (Saturday or Sunday) using the calendar.weekday() function −

import calendar

def is_weekend(year, month, day):
   return calendar.weekday(year, month, day) in (5, 6)

print("Is March 16, 2025 a weekend?:", is_weekend(2025, 3, 16))

We get the output as shown below −

Is March 16, 2025 a weekend?: True  # (Sunday)

Example: Finding the First Day of a Month

We can use calendar.weekday() function to find the weekday of the first day of a given month −

import calendar

year = 2025
month = 7

first_day = calendar.weekday(year, month, 1)
print("First day of July 2025:", first_day)

The result produced is as shown below −

First day of July 2025: 1  # (Tuesday)
python_date_time.htm
Advertisements