Fundamentals 6 min read

Six Ways to Get the Weekday of a Specific Date in Python

This tutorial explains six Python techniques—including datetime.weekday(), datetime.isoweekday(), strftime(), calendar, pendulum, and pandas—to retrieve the weekday of any given date such as 2022‑02‑22, with code examples and usage tips for data analysis.

IT Services Circle
IT Services Circle
IT Services Circle
Six Ways to Get the Weekday of a Specific Date in Python

When performing data analysis in Python, grouping by date often requires knowing the weekday of a specific date. Using the example date 2022‑02‑22, this article presents six methods to obtain the weekday.

datetime.weekday()

The built‑in datetime module provides the weekday() method, which returns an integer from 0 (Monday) to 6 (Sunday). Example:

from datetime import datetime
datetime.date(2022, 2, 22).weekday()
# Output: 1

To map the integer to a Chinese weekday name, define a list and index it:

week_list = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
week_list[datetime.date(2022, 2, 22).weekday()]
# Output: '星期二'

datetime.isoweekday()

The isoweekday() method works similarly but returns 1 (Monday) to 7 (Sunday):

import datetime
datetime.date(2022, 2, 22).isoweekday()
# Output: 2

datetime.strftime()

Use strftime() to format a date as a string. The format code %A yields the full English weekday name, while %a gives the abbreviated name:

import datetime
datetime.date(2022, 2, 22).strftime("%A")
# Output: 'Tuesday'

datetime.date(2022, 2, 22).strftime("%a")
# Output: 'Tue'

calendar module

The standard calendar module also offers weekday() and day_name for weekday lookup:

import calendar
calendar.weekday(2022, 2, 22)
# Output: 1

date_week = datetime.date(2022, 2, 22).weekday()
calendar.day_name[date_week]
# Output: 'Tuesday'

pendulum library

For more advanced date handling, the third‑party pendulum library can be used. Its day_of_week attribute returns the ISO weekday number:

import pendulum
pendulum.parse('2022-02-22').day_of_week
# Output: 2

Pandas

When working with tabular data, Pandas provides convenient accessors. The Series.dt.day_name() method returns the weekday name for each datetime entry:

import pandas as pd
from datetime import datetime

df = pd.DataFrame({
    "name": ["张三", "李四", "朱五"],
    "date": [datetime(2022, 2, 21), datetime(2022, 2, 22), datetime(2022, 2, 23)]
})

df["week_name"] = df["date"].dt.day_name()
print(df)
# Shows the weekday names for each row

By covering these six approaches— datetime.weekday() , datetime.isoweekday() , datetime.strftime() , calendar , pendulum , and Pandas —the article equips readers with multiple tools to retrieve weekday information in Python for data analysis tasks.

PythonDateTimepandascalendarpendulumweekday
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.