A Problem Partially Solved: Day of the Week
In this lesson, we will use arithmetic expressions to ascertain the day of the week given its calendar date.
We'll cover the following
Problem statement
Various devices in our homes record the current date as the month, day, and year. When a date is displayed, it often includes the day of the week. Is it possible to ascertain the day of the week from the month, day, and year? The answer is yes, as you will soon see.
Discussion
An algorithm known as Zeller’s congruence makes the determination that we need. According to this algorithm, a date such as December 31, 2020
, is described as three components, as follows:
 m is the month number, but one that is slightly different from what we are used to. March is month
3
, April is month4
, and so on to December, which is month12
, but the algorithm considers January and February as months13
and14
of the preceding year. Thus, for these months, we subtract1
from the year before continuing.  d is the day of the month.
 y is the year, which was possibly adjusted earlier.
Here are two examples:

December 31, 2020
: m is12
, d is31
, and y is2020
. 
January 2, 1800
: m is13
, d is2
, and y is1799
.

Next, we compute:
f = d + [(26 × (m + 1)) / 10] + y + [y / 4] + 6 × [y / 100] + [y / 400]
where the square brackets indicate that the division is truncated to an integer. We now compute w = f modulo 7
A modulo 7
number system counts from 0
to 6
and then starts over again at 0
. Since f is not negative, we compute w by getting the remainder after dividing f by 7
. That is, we use the Java operator %
. The result w represents the day of the week of the given date, as follows: 0
indicates a Saturday, 1
indicates a Sunday, 2
indicates a Monday, and so on.
The computation for December 31, 2020
, proceeds as follows. Since m is 12
, 26 × (m + 1)
is 338
. Divide this number by 10
and ignore the fraction to get 33
. Add 31
for d and 2020 for y to get 2084
. Then [y / 4]
is 505
, 6 × [y / 100]
is 120
, and [y / 400]
is 5
. Adding 505
, 120
, and 5
to 2084
, we find that f is 2714
. Divide f by 7
and get a quotient of 387
and a remainder of 5
. This remainder is w and represents Thursday.
Get handson with 1200+ tech skills courses.