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.
Table of Contents
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 value0
- Another method, the
isoweekday
, assigns Sunday the value of1
Check out the official 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
# Define a list of weekday names days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Index using weekday int value days[date.weekday[]] >>> Sunday1 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 .
Pros:
- No extra dependencies
- One-liner possible
- Semi-flexible
- No fear of typos
- Easy to integrate project-wide
Cons:
- Need to be familiar with
# Define a list of weekday names days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Index using weekday int value days[date.weekday[]] >>> Sunday
3 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
# Define a list of weekday names days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Index using weekday int value days[date.weekday[]] >>> Sunday4 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
# Define a list of weekday names days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Index using weekday int value days[date.weekday[]] >>> Sunday5 to
# Define a list of weekday names days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Index using weekday int value days[date.weekday[]] >>> Sunday6—it seemed like wading into the deep end for what was such a peripheral task.