Home Python Datetime Methods weekday() Python date.weekday() Method Definition weekday() returns the day of the week as an int : 0 for Monday, 1 for Tuesday, up to 6 for Sunday.
The numbering matches the calendar module constants (calendar.MONDAY is 0), and it is one less than isoweekday() . Because it is a small int, it works well with modulo arithmetic for "next Monday" style calculations.
date.weekday() is an instance method of datetime.date: call it on an existing object with dot notation, such as day.weekday(...). Import the class first, for example from datetime import date. The datetime class has a method with the same name.
Syntax Call it with no arguments.
Call What it does date(2024, 1, 1).weekday()Returns 0: Monday. datetime(2024, 3, 15, 23, 59).weekday()Returns 4: Friday; the time is ignored. date(2024, 3, 17).weekday()Returns 6: Sunday.
Weekend test: day.weekday() >= 5.
Method signatures date.weekday()
datetime.weekday()
# returns: int, 0 (Monday) .. 6 (Sunday) Parameters weekday() takes no parameters.
Return Value Return Value Description int 0 for Monday through 6 for Sunday.
Important
strftime("%w") counts differently (Sunday 0, Saturday 6), and JavaScript's getDay() uses that Sunday-first scale too. For aware datetimes the result belongs to the object's own wall-clock date, which may differ from the UTC date.
Python Examples 1. Weekday numbers 2. Next given weekday 3. Last Friday of a month Example 1 — Get Weekday Numbers for Fixed Dates A Monday, a Friday late at night, and a Sunday.
from datetime import date, datetime
print(date(2024, 1, 1).weekday())
print(datetime(2024, 3, 15, 23, 59).weekday())
print(date(2024, 3, 17).weekday()) How It Works Monday first. 1 January 2024 is a Monday, so the result is 0.Time ignored. 23:59 on Friday is still Friday, number 4.Sunday last. Sunday is the highest number, 6.Example 2 — Find the Next Monday, Friday, or Saturday Use modulo 7 to count the days until the next target weekday, always moving forward at least one day.
from datetime import date, timedelta
def next_weekday(start, target):
days_ahead = (target - start.weekday()) % 7 or 7
return start + timedelta(days=days_ahead)
friday = date(2024, 3, 15)
print(next_weekday(friday, 0))
print(next_weekday(friday, 4))
print(next_weekday(friday, 5)) 2024-03-18
2024-03-22
2024-03-16 How It Works Distance with modulo. (0 - 4) % 7 is 3, so the next Monday is three days after Friday.Never today. When the target is the same weekday, the modulo gives 0 and or 7 jumps a full week.Tomorrow. Saturday (5) is one day after Friday (4).Example 3 — Find the Last Friday of Each Month Step back from the last day of the month to the nearest Friday.
from datetime import date, timedelta
def last_friday(year, month):
first_next = date(year + month // 12, month % 12 + 1, 1)
last_day = first_next - timedelta(days=1)
return last_day - timedelta(days=(last_day.weekday() - 4) % 7)
for month in (1, 2, 12):
print(last_friday(2024, month)) 2024-01-26
2024-02-23
2024-12-27 How It Works Last day of the month. The first day of the next month minus one day handles 28, 29, 30, and 31-day months.December wraps. month // 12 and month % 12 + 1 turn December 2024 into 1 January 2025.Step back to Friday. (weekday - 4) % 7 is how many days to go back; for Wednesday 31 January it is 5, giving 26 January.Frequently Asked Questions What does weekday() return for Sunday? 6. Monday is 0.
How do I get the weekday name from weekday()? Use calendar.day_name[day.weekday()] or day.strftime("%A"); both follow the locale.
How do I check if a date is a weekday (not weekend)? day.weekday() < 5.
Why does weekday() differ from JavaScript getDay()? getDay() starts with Sunday as 0. Python weekday() starts with Monday as 0.
Which is better, weekday() or isoweekday()? Both are fine. weekday() suits list indexes and modulo math; isoweekday() matches ISO 8601 and human numbering.
Back to the method index See all 29 datetime method guides grouped by what they do.
Method Index → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion