In this post, I will show you how you can calculate the day of the week for any date using Zeller’s formula.
The formula was proposed by a Reverend Zeller.
Zeller's congruence is an algorithm devised by Christian Zeller to calculate the day of the week for any Julian or…
Here’s the formula.
f = k + [(13*m-1)/5] + D + [D/4] + [C/4] - 2*C.
Note [x] stands for the greatest integer smaller than or equal to x. For eg. [5.69] = 5.
Let us suppose we have to calculate the day of the week for 31st August 2019.
- k is the day of the month. For the given example k = 31.
- m is the month number when starting counting from March. March is marked as 1, April as 2, and so on. Therefore January is marked as 1 and February is 2. For the given example m = 6.
- D is the last two digits of the year. If the month is January or February, then D is subtracted by 1. For the given example D = 19 as the month is August. For 31st January 2019, D = 18.
- C is the first two digits of the year. For the given example C = 20.
Now, let us calculate the value of
f for the above date.
f = k + [(13*m-1)/5] + D + [D/4] + [C/4] - 2*C
= 31 + [(13*6-1)/5] + 19 + [19/4] + [20/4] - 2*20
= 31 + [15.4] + 19 + [4.75] +  - 40
= 31 + 15 + 19 + 4 + 5 - 40
Now that we know the value of
f let us divide it by 7 to calculate the remainder. Note, that if the remainder is negative, we add 7 to make it positive.
34 % 7 // finding remainder
A remainder of 0 denotes Sunday, 1 means Monday, etc. So for our example, 31st August 2019 would be Saturday.
This problem is available on LeetCode.
Day of the Week - LeetCode
Given a date, return the corresponding day of the week for that date. The input is given as three integers representing…
Here’s the code to implement the same in Java.