date calculation
day of the week
algorithm
calendar
time management

How to determine day of week by passing specific date?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In many programming and mathematical tasks, determining the day of the week for a given date is a common requirement. This can be achieved through various algorithms and techniques, some rooted in historical mathematical methods, while others are more programmatically inclined. This article delves into some of these methods, providing technical explanations and examples to illuminate the process.

Understanding the Calendar System

Before diving into the algorithms, it's important to understand the Gregorian calendar, which is the calendar system used by most of the world today:

  • Weekdays: The days of the week are Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.
  • Gregorian Calendar: Introduced in October 1582 by Pope Gregory XIII, this is a modification of the Julian calendar aimed at more accurately tracking the solar year.

Zeller's Congruence

One of the most renowned algorithms for computing the day of the week is Zeller's Congruence. It is an efficient and straightforward method, especially suited for calculating weekday names given any date on the Gregorian or Julian calendar.

Zeller's Congruence Formula

For the Gregorian calendar:

h=(q+13(m+1)5+K+K4+J4+5J)mod7h = (q + \lfloor \frac{13(m + 1)}{5} \rfloor + K + \lfloor \frac{K}{4} \rfloor + \lfloor \frac{J}{4} \rfloor + 5J) \mod 7

Where:

  • h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday).
  • q is the day of the month.
  • m is the month (3 = March, 4 = April, ..., 14 = February)
  • K is the year of the century (year % 100).
  • J is the zero-based century (year / 100).

Important Adjustments

  • January and February are counted as months 13 and 14 of the previous year. For example, February 2023 would be treated as the 14th month of 2022.

Example

Let’s calculate which day of the week November 11, 2023, falls on:

  1. Start with: q = 11, m = 11, year = 2023
  2. K = 23 (2023 % 100)
  3. J = 20 (2023 / 100)
  4. Substitute in the formula:

h=(11+13×125+23+234+204+5×20)mod7h = (11 + \lfloor \frac{13 \times 12}{5} \rfloor + 23 + \lfloor \frac{23}{4} \rfloor + \lfloor \frac{20}{4} \rfloor + 5 \times 20) \mod 7

  1. Compute each component:
    • 13×125=31\lfloor \frac{13 \times 12}{5} \rfloor = 31
    • 234=5\lfloor \frac{23}{4} \rfloor = 5
    • 204=5\lfloor \frac{20}{4} \rfloor = 5
  2. Calculating h gives:

h=(11+31+23+5+5+100)mod7=175mod7=0h = (11 + 31 + 23 + 5 + 5 + 100) \mod 7 = 175 \mod 7 = 0

0 corresponds to Saturday, so November 11, 2023, is a Saturday.

Python Implementation

To bring a practical perspective, here is an implementation of Zeller's Congruence in Python:

python
1def day_of_week(year, month, day):
2    if month < 3:
3        month += 12
4        year -= 1
5    K = year % 100
6    J = year // 100
7    h = (day + (13 * (month + 1)) // 5 + K + K // 4 + J // 4 + 5 * J) % 7
8    day_mapping = {0: "Saturday", 1: "Sunday", 2: "Monday", 3: "Tuesday", 4: "Wednesday", 5: "Thursday", 6: "Friday"}
9
10    return day_mapping[h]
11
12print(day_of_week(2023, 11, 11))  # Output: Saturday

Other Methods

Doomsday Algorithm

Developed by John Horton Conway, the Doomsday Algorithm is another method used to calculate the day of the week:

  1. Anchor Day: Determine the anchor day for centuries.
  2. Doomsday for Year: Calculate a "doomsday" for each year.
  3. Day Calculation: Compare target date with the nearest doomsday.

This method relies on known "doomsdays" of a year, such as the last day of February, which simplifies the counting process.

Understanding Leap Years

Leap years are integral to calendar calculations. A leap year in the Gregorian calendar is:

  • Divisible by 4;
  • Not divisible by 100, unless also divisible by 400.

This sequence affects February’s length (29 days in leap years).

Key Points Summary

Key ConceptDescription
Gregorian CalendarModern solar calendar with 365 days, leap year adjustment.
Zeller's CongruenceFormula to calculate day of the week.
Weekday Mapping0: Saturday, 1: Sunday, 2: Monday, ..., 6: Friday.
Month AdjustmentJanuary and February treated as month 13 & 14 of the previous year.
Century and Year CalcK = year % 100, J = year / 100.
Python ImplementationProvides a practical coding example to determine the day of the week.

These methods and explanations provide a comprehensive guide to determining the day of the week for any given date. Whether using traditional mathematical methods or modern programming languages, the task is made accessible and efficient through these well-established techniques.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.