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.
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:
Where:
his the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday).qis the day of the month.mis the month (3 = March, 4 = April, ..., 14 = February)Kis the year of the century (year % 100).Jis 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:
- Start with:
q = 11,m = 11,year = 2023 K = 23(2023 % 100)J = 20(2023 / 100)- Substitute in the formula:
- Compute each component:
- Calculating
hgives:
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:
Other Methods
Doomsday Algorithm
Developed by John Horton Conway, the Doomsday Algorithm is another method used to calculate the day of the week:
- Anchor Day: Determine the anchor day for centuries.
- Doomsday for Year: Calculate a "doomsday" for each year.
- 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 Concept | Description |
| Gregorian Calendar | Modern solar calendar with 365 days, leap year adjustment. |
| Zeller's Congruence | Formula to calculate day of the week. |
| Weekday Mapping | 0: Saturday, 1: Sunday, 2: Monday, ..., 6: Friday. |
| Month Adjustment | January and February treated as month 13 & 14 of the previous year. |
| Century and Year Calc | K = year % 100, J = year / 100. |
| Python Implementation | Provides 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
- How to determine if a Delaunay triangle is internal or external?
- How to determine if a linked list has a cycle using only two memory locations
- How to determine if a list is subset of another list?
- How to determine if a sequence is bitonic?
- How to determine if binary tree is balanced?
- how to determine maximum route cost in a n high numeric pyramid
- How to determine memory and time complexity of an algorithm?
- How to determine simplex time complexity ie Max flow

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 courseTrack 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.