Getting the Weekday Name in Python

python weekday name datetime

Python’s DateTime module offers a wide range of time-handling functions that make life a breeze. However, there’s no direct method to return a plain-English representation of a weekday name. Fortunately, there are several easy ways to get there.

Getting the Date + Weekday

Before we take a look at getting the weekday name, let’s consider how Python’s datetime class gets a date and, subsequently, determines the day of the week for said date. There’s nothing fancy about it—but it pays to know where you’re starting.

from datetime import datetime

# Create datetime object
date = datetime.now()
>>> 2021-02-07 15:30:46.665318

# Get the weekday value, as an integer
date.weekday()
>>> 6

This code illustrates how far the datetime class will get you before you have to start getting creative.  Before we move on, there are two things worth noting:

  • The weekday method assigns Monday the value 0
  • Another method, the isoweekday, assigns Sunday the value of 1

Check out the official datetime.weekday documentation for more info. Now, knowing how to get an integer representation of the weekday for a given datetime object, we can start scheming on how to produce a plain-English version. Below are three approaches ranked in order of dependencies.

Method 1: Do it Manually

# Define a list of weekday names
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

# Index using weekday int value
days[date.weekday()]

>>> Sunday

This approach offers the flexibility of being able to tweak weekday naming and order on-the-fly. Need to truncate the date names? Just edit the list. Need to start with Sunday? Just re-arrange it. Need to reverse the list for some strange reason? Just reverse it!

Pros:

  • No added dependencies
  • No added methods to remember
  • Flexibility for change
  • Explicit representation in code

Cons:

  • More typing
  • Not immediately resistant to duplication issues (i.e. using it across a project)
  • The fear of typos

Method 2: Use Strftime

# Convert the integer to english representation
date.date().strftime("%A")

>>> Sunday

This method is, arguably, the most succinct. It’s a reasonable one-liner, doesn’t stray from the datetime class, and offers some flexibility once familiar with the strftime documentation.

Pros:

  • No extra dependencies
  • One-liner possible
  • Semi-flexible
  • No fear of typos
  • Easy to integrate project-wide

Cons:

  • Need to be familiar with strftime to change format

Method 3: The Calendar Module

import calendar

# Convert using calender's day_name method
calender.day_name[date.weekday()]

>>> Sunday

This is really just a round-about way to achieve the same result as the first method. The added benefit here is that someone else manages the weekday list (not really a burden in this case.) The calender.day_name object is just a fancy list of all weekday names. Indexing into this object is the same as indexing into a manual list.

Pros:

  • Built-in module
  • Lightweight dependency
  • Uses native Python syntax for indexing

Cons:

  • Extra dependency
  • The syntax could be simpler

Final Thoughts

When I was first learning to code, one of the biggest surprises to me was how complex managing time and dates can be. The subtle length differences in months, leap years, converting strings to datetimes—it seemed like wading into the deep end for what was such a peripheral task.

Converting computer-friendly date representations like epoch timestamps into user-friendly representations is another annoyingly demanding task. In this case, Python’s datetime class makes it simple to get the weekday integer. After that, it’s all just syntactic sugar! Personally, I prefer method two here because it keeps everything tightly coupled to the DateTime module.

Zαck West
Full-Stack Software Engineer with 10+ years of experience. Expertise in developing distributed systems, implementing object-oriented models with a focus on semantic clarity, driving development with TDD, enhancing interfaces through thoughtful visual design, and developing deep learning agents.