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.
SyntaxFollowing is the syntax of the Python calendar.weekday() function −
weekday = calendar.weekday(year, month, day)Parameters
This function accepts the following parameters −
This function returns an integer representing the weekday:
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
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