In 1970, John Horton Conway devised an algorithm, often termed the “Doomsday Algorithm,” to quickly calculate the day of the week using simple mental math.
This algorithm uses the formula: (d + m + y + [y/4] + c) mod 7
Where d is the day, m is the month, y is the year, and c is the century number.
Each weekday is assigned a number using modulo 7. In ISO 8601 standards, Monday is 1 and Sunday is 7.
Understanding the Calendar
A common year has 365 days, and a leap year has 366. Since 365 mod 7 = 1, each new year starts on the next weekday compared to the previous year, unless it's a leap year.
Some months start on the same day of the week. For example, April and July 2016 both started on a Friday because the days between them (91) is divisible by 7.

Several months begin on the same weekday as others in the same year.
For Common Years:
- January and October
- February, March, and November
- April and July
- No month corresponds with August

For Leap Years:
- January, April, and July
- February and August
- March and November
- No month corresponds with October

Tomohiko Sakamoto's Implementation
Sakamoto extended the Doomsday Algorithm to factor in leap years.
Using month-day combinations and modulo arithmetic, you get a consistent pattern. For example:
- January has 31 days → 31 mod 7 = 3 → February 1 is 3 days after January 1
- January + February = 59 days → 59 mod 7 = 3 → March 1 is also 3 days after January 1
This gives us the list: {0,3,3,6,1,4,6,2,5,0,3,5}
To account for leap years, use: y/4 - y/100 + y/400
. For months before March, decrement the year by 1: y -= m < 3
This changes the month values to: {0,3,2,5,0,3,5,1,4,6,2,4}
C++ Implementation
int dow(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
Python Implementation
def day_of_week(year, month, day):
t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
year -= month < 3
return (year + year // 4 - year // 100 + year // 400 + t[month - 1] + day) % 7
With this, determining the weekday for any date becomes a quick and fun mental trick.