How to find the day of the week for any Date?

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.

Here’s the formula.

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.

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.

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.

Here’s the code to implement the same in Java.

Written by

Grad Student at ASU | Student Researcher at The Luminosity Lab | Ex Senior Software Engineer, Zeta | Volunteer, Wikimedia Foundation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store