Use .strftime("%A") (or .strftime("%a") for abbreviated names) on a datetime.date / datetime.datetime object to get the localized weekday name (such as "Sunday"), or call .weekday() (Monday = 0 … Sunday = 6) / .isoweekday() (Monday = 1 … Sunday = 7) when you need a numeric index for sorting, array indexing, or business-logic comparisons.
Basic Example
from datetime import date, datetime
d = date(2026, 11, 29) # Or datetime.now() / date.today()
# 1. Weekday name via strftime():
print(d.strftime("%A")) # Output: 'Sunday' (full weekday name)
print(d.strftime("%a")) # Output: 'Sun' (abbreviated weekday name)
# 2. Numeric weekday index (language-independent):
print(d.weekday()) # Output: 6 (Monday=0 ... Sunday=6)
print(d.isoweekday()) # Output: 7 (Monday=1 ... Sunday=7, ISO 8601 standard)
# 3. Parsing an ISO date string first, then extracting the day of the week:
parsed = date.fromisoformat("2026-11-29")
print(parsed.strftime("%A")) # Output: 'Sunday'
Key Details & Edge Cases
.weekday()vs..isoweekday()off-by-one trap:.weekday()is 0-indexed starting on Monday (0= Monday,6= Sunday), making it ideal for indexing into Python lists likecalendar.day_name[d.weekday()]..isoweekday()is 1-indexed starting on Monday (1= Monday,7= Sunday), matching the ISO 8601 standard used in SQL databases and international scheduling.
- Locale sensitivity of
.strftime("%A")vs.calendar.day_name:.strftime("%A")respects the current CLC_TIMElocale (locale.setlocale), returning"Dimanche"under a French locale or"Sunday"underC/en_US. If you always want English weekday names regardless of system locale settings, index intocalendar.day_name[d.weekday()](orcalendar.day_abbr[d.weekday()]).
- Getting the day of the week across a Pandas
Series/DataFramecolumn:- Never loop row-by-row with
.apply(lambda d: d.strftime("%A"))on Pandas data—use the vectorized.dtaccessor instead:import pandas as pd s = pd.to_datetime(pd.Series(["2026-11-29", "2026-11-30"])) print(s.dt.day_name()) # Series: ['Sunday', 'Monday'] print(s.dt.dayofweek) # Series: [6, 0] (Monday=0 ... Sunday=6)
- Never loop row-by-row with
- Time-zone awareness when calling
datetime.now():- A naive
datetime.now()uses the server’s local clock (often UTC in cloud containers), which can report yesterday’s or tomorrow’s weekday around midnight. Pass an explicitzoneinfo.ZoneInfo(...)(datetime.now(ZoneInfo("Asia/Karachi")).strftime("%A")) when the user’s local calendar date matters.
- A naive
Comparison of Methods to Get the Day of the Week in Python
| Method | Return Value for Sunday | Return Type | Locale-Dependent? | Best Used When |
|---|---|---|---|---|
d.strftime("%A") / "%a" |
"Sunday" / "Sun" |
str |
Yes (uses active LC_TIME locale) |
Formatting human-readable dates in logs, reports, and UI strings. |
d.weekday() |
6 (Mon=0 … Sun=6) |
int |
No | Zero-based indexing into lists/tuples or checking weekends (d.weekday() >= 5). |
d.isoweekday() |
7 (Mon=1 … Sun=7) |
int |
No | ISO 8601 week calculations (d.isoweekday() in {6, 7}) and database compatibility. |
calendar.day_name[d.weekday()] |
"Sunday" |
str |
Uses calendar locale (English by default) |
Generating weekday lookup lists or English day names independent of OS strftime quirks. |
df["date"].dt.day_name() |
"Sunday" |
pd.Series |
Optional locale= param |
Vectorized weekday extraction across thousands or millions of Pandas rows. |
You must log in or register to comment.
