Learn how to determine the day of the week using the Zeller's Congruence in JavaScript.

Surely, the usage of a library in Moment.js is the best option to determine which day of the week it was in a specific date, we don't recommend you to do it in other way. However, for educational purposes, specially in the university you will need for a task of your professor, to determine which day of the week it is on a specific day (in the past and from now on). The easiest programming solution to solve this issue, is through the Zeller's congruence.

Zeller's congruence is a way to work out what day of the week any day of any year fell on. Most commonly it is used to find on what day you were born (which can determine your character according to the old rhyme above). 

In this short article, we will explain you how to implement and how to understand the Zeller's Formula for a gregorian calendar in JavaScript easily.

JavaScript implementation of the Zeller's Congruence

In the Zeller's Formula, the month Starts from March, so:

  • March = 1,
  • April = 2,
  • May = 3
  • and so on… till
  • Dec = 10,
  • Jan = 11
  • Feb. = 12
  • So, for 06/08/1990, M=6

Let day number = D, month number = M year = Y. If M is 1 or 2, add 12 to M, and subtract 1 from Y. Let C be the first two digits of Y and K be the last two digits of Y. Add together the integer parts of (2.6M—5.39), (K/4) and (C/4). Add to this D and K, and subtract 2C. (The integer part of a number is the whole number part: integer part of 2.3 is 2, and of 6.7 is 6. Note that the integer part of –1.7 is –2. This can be done with the Math.floor function of JavaScript).

Find the remainder when this number is divided by 7. If this remainder is 0 the day is Sunday, 1 is Monday, 2 is Tuesday, etc. The implementation in JavaScript of this logic is the following:

/**
 * Function that implements the zellers congruence and returns according to the result the day of the week.
 * 
 * @param D {Int} Number of the day (0-31)
 * @param M {Int} Number of the month (1-12)
 * @param Y {Year} Whole year e.g 2001
 */
function Zeller(D, M, Y){    
    var Day = "";

    if (M < 3)
    {
        M = M + 12;
        Y = Y - 1;
    }
    
    var C = Math.floor(Y / 100);
	var K = Y - (100 * C);
	
	var S = Math.floor(2.6 * M - 5.39) + Math.floor(K / 4) + Math.floor(C / 4) + D + K - (2 * C);
	
    var ans = S - (7 * Math.floor(S / 7));
    
    if (ans == 0)
    {
        Day = "Sunday";
    }
    else if (ans == 1)
    {
        Day = "Monday";
    }
    else if (ans == 2)
    {
        Day = "Tuesday";
    }
    else if (ans == 3)
    {
        Day = "Wednesday";
    }
    else if (ans == 4)
    {
        Day = "Thursday";
    }
    else if (ans == 5)
    {
        Day = "Friday";
    }
    else
    {
        Day = "Saturday";
    }
    
    return Day;
}

Our zeller function will return a string that returns the day of the week that corresponds to the date, for example:

// Thursday
Zeller(19,11, 2020);

// Tuesday
Zeller(14,05, 2019);

// Wednesday
Zeller(5, 2, 1670);

// .. and so on

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors