Python: How to find the day of the week for any date (3 methods)

Updated: February 13, 2024 By: Guest Contributor Post a comment

Introduction

Finding the day of the week for any given date is a common problem in software development. Whether you’re generating schedules, reports, or you’re just simply curious about what day of the week your birthday falls on a particular year, Python has got your back. This tutorial will guide you through various methods to solve this problem efficiently.

First, it’s essential to understand that Python, via its standard library, provides robust modules such as datetime and calendar, which offer straightforward ways to find the day of a week. Furthermore, we’ll also look into a custom algorithm for educational purposes.

Method 1: Using datetime module

The datetime module in Python is versatile for handling dates and times. Here’s how you can use it to find the day of the week:

from datetime import datetime

# Specify the date in 'YYYY-MM-DD' format
specified_date = '2023-04-23'

date_obj = datetime.strptime(specified_date, '%Y-%m-%d')

# strftime('%A') returns the full name of the weekday
day_of_week = date_obj.strftime('%A')

print(f'The day of the week is: {day_of_week}')

This method is direct and efficient for most applications. You simply format the `date_obj` to the string representing the full name of the weekday using `%A`.

Method 2: Using the calendar module

The calendar module provides another approach to find the day of the week. It offers a function weekday() which returns the day of the week as an integer, where Monday is 0 and Sunday is 6:

import calendar

# Specify the date in 'YYYY-MM-DD' format
specified_date = '2023-04-23'

year, month, day = map(int, specified_date.split('-'))

# Using weekday() to get the day of the week as an integer
day_of_week_num = calendar.weekday(year, month, day)

# Convert the integer to the actual day name
week_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
day_of_week = week_days[day_of_week_num]

print(f'The day of the week is: {day_of_week}')

This method gives you more control over the format of the day of the week since you can customize the output based on the integer value.

Method 3: Custom Algorithm

For educational purposes, or in scenarios where you might not want to rely on external libraries, you can implement your algorithm. One popular algorithm for this problem is Zeller’s Congruence. While the implementation can seem overwhelming at first, it provides a deeper understanding of how weekdays can be calculated.

Here is a basic implementation of Zeller’s Congruence in Python:

def zellers_congruence(day, month, year):
    if month < 3:
        month += 12
        year -= 1
    k = year % 100
    j = year // 100
    f = day + ((13 * (month + 1)) // 5) + k + (k // 4) + (j // 4) - (2 * j)
    return ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'][f % 7]

specified_date = '2023-04-23'
year, month, day = map(int, specified_date.split('-'))
day_of_week = zellers_congruence(day, month, year)
print(f'The day of the week is: {day_of_week}')

While Zeller’s Congruence is not typically necessary for application development thanks to the well-equipped datetime and calendar modules, understanding it can be quite rewarding and insightful.

Concluding Thoughts

Finding the day of the week for any given date in Python is a straightforward task thanks to the powerful standard library. Whether you opt for the simplicity of the datetime module, the control offered by the calendar module, or even dive into custom algorithms like Zeller’s Congruence, Python offers multiple paths to reach the solution. As with many Python tasks, the language’s flexibility and robust standard library make it an excellent choice for date and time manipulations.

Understanding these methods not only solves the immediate problem of finding the day of the week but also opens up a broader perspective on date handling in software development. So, apply these methods as needed and don’t shy away from exploring further into Python’s date and time capacities!