Open In App
Related Articles

Zeller’s Congruence | Find the Day for a Date

Improve Article
Improve
Save Article
Save
Like Article
Like

Zeller’s congruence is an algorithm devised by Christian Zeller to calculate the day of the week for any Julian or Gregorian calendar date. It can be considered to be based on the conversion between Julian’s day and the calendar date. 
It is an algorithm to find the day of the week for any date. 
For the Gregorian calendar it is: 
h=\left ( q + [ \frac{13(m+1)}{5} ]+K +\left [ \frac{K}{4}\right ]+\left [ \frac{J}{4}\right ]+5J\right )mod7
For the Julian calendar it is: 
h=\left ( q + \left [ \frac{13(m+1)}{5} \right ]+K +\left [ \frac{K}{4}\right ]+5-J\right )mod7
where, 

  1. h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, …, 6 = Friday)
  2. q is the day of the month
  3. m is the month (3 = March, 4 = April, 5 = May, …, 14 = February)
  4. K is the year of the century (year % 100).
  5. J is the zero-based century (actually ? year/100 ?) For example, the zero-based centuries for 1995 and 2000 are 19 and 20 respectively (to not be confused with the common ordinal century enumeration which indicates 20th for both cases).
NOTE: In this algorithm January and February are
      counted as months 13 and 14 of the previous
      year.E.g. if it is 2 February 2010, the 
      algorithm counts the date as the second day 
      of the fourteenth month of 2009 (02/14/2009 
      in DD/MM/YYYY format)

For an ISO week date Day-of-Week d (1 = Monday to 7 = Sunday), use  

 d = ((h+5)%7) + 1 

C++




// C++ program to Find the Day
// for a Date
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
 
int Zellercongruence(int day, int month, int year)
{
    if (month == 1) {
        month = 13;
        year--;
    }
    if (month == 2) {
        month = 14;
        year--;
    }
    int q = day;
    int m = month;
    int k = year % 100;
    int j = year / 100;
    int h
        = q + 13 * (m + 1) / 5 + k + k / 4 +
                              j / 4 + 5 * j;
    h = h % 7;
    switch (h) {
    case 0:
        cout << "Saturday \n";
        break;
    case 1:
        cout << "Sunday \n";
        break;
    case 2:
        cout << "Monday \n";
        break;
    case 3:
        cout << "Tuesday \n";
        break;
    case 4:
        cout << "Wednesday \n";
        break;
    case 5:
        cout << "Thursday \n";
        break;
    case 6:
        cout << "Friday \n";
        break;
    }
    return 0;
}
 
// Driver code
int main()
{
    Zellercongruence(22, 10, 2017); // date (dd/mm/yyyy)
    return 0;
}


Java




// Java program to  Find the Day
// for a Date
import java.util.*;
 
class GFG
{
    // Print Day for a Date
    static void Zellercongruence(int day, int month,
                                 int year)
    {
        if (month == 1)
        {
            month = 13;
            year--;
        }
        if (month == 2)
        {
            month = 14;
            year--;
        }
        int q = day;
        int m = month;
        int k = year % 100;
        int j = year / 100;
        int h = q + 13*(m + 1) / 5 + k + k / 4 + j / 4 + 5 * j;
        h = h % 7;
        switch (h)
        {
            case 0 : System.out.println("Saturday"); break;
            case 1 : System.out.println("Sunday"); break;
            case 2 : System.out.println("Monday"); break;
            case 3 : System.out.println("Tuesday"); break;
            case 4 : System.out.println("Wednesday"); break;
            case 5 : System.out.println("Thursday"); break;
            case 6 : System.out.println("Friday"); break;
        }
    }
     
    // Driver code
    public static void main(String[] args)
    {
        Zellercongruence(22, 10, 2017); //date (dd/mm/yyyy)
    }
}
 
/* This code is contributed by Mr. Somesh Awasthi */


Python3




# Python3 program to  Find the Day
# for a Date
 
def switch(h) :
    return {
        0 : "Saturday",
        1 : "Sunday",
        2 : "Monday",
        3 : "Tuesday",
        4 : "Wednesday",
        5 : "Thursday",
        6 : "Friday",
    }[h]
 
def Zellercongruence(day, month, year) :
    if (month == 1) :
        month = 13
        year = year - 1
 
    if (month == 2) :
        month = 14
        year = year - 1
    q = day
    m = month
    k = year % 100;
    j = year // 100;
    h = q + 13 * (m + 1) // 5 + k + k // 4 + j // 4 + 5 * j
    h = h % 7
    print(switch (h))
     
     
     
# Driver code
Zellercongruence(22, 10, 2017) #date (dd/mm/yyyy)
 
# This code is contributed by Nikita Tiwari


C#




// C# program to  Find the Day
// for a Date
using System;
 
class GFG {
     
    // Print Day for a Date
    static void Zellercongruence(int day,
                      int month, int year)
    {
        if (month == 1)
        {
            month = 13;
            year--;
        }
        if (month == 2)
        {
            month = 14;
            year--;
        }
        int q = day;
        int m = month;
        int k = year % 100;
        int j = year / 100;
        int h = q + 13 * (m + 1) / 5 + k + k / 4
                                 + j / 4 + 5 * j;
        h = h % 7;
        switch (h)
        {
            case 0 : Console.WriteLine("Saturday");
                     break;
                      
            case 1 : Console.WriteLine("Sunday");
                     break;
                      
            case 2 : Console.WriteLine("Monday");
                     break;
                      
            case 3 : Console.WriteLine("Tuesday");
                     break;
                      
            case 4 : Console.WriteLine("Wednesday");
                     break;
                      
            case 5 : Console.WriteLine("Thursday");
                     break;
                      
            case 6 : Console.WriteLine("Friday");
                     break;
        }
    }
     
    // Driver code
    public static void Main()
    {
         
        //date (dd/mm/yyyy)
        Zellercongruence(22, 10, 2017);
    }
}
 
/* This code is contributed by vt_m */


PHP




<?php
// PHP program to Find the Day
// for a Date
 
function Zellercongruence($day, $month, $year)
{
    if ($month == 1)
    {
        $month = 13;
        $year--;
    }
     
    if ($month == 2)
    {
        $month = 14;
        $year--;
    }
     
        $q = $day;
        $m = $month;
        $k = $year % 100;
        $j = $year / 100;
        $h = $q + 13*($m + 1) / 5 + $k +
               $k / 4 + $j / 4 + 5 * $j;
        $h = $h % 7;
    switch ($h)
    {
        case 1 : echo "Saturday \n";
        break;
        case 2 : echo "Sunday \n";
        break;
        case 3 : echo "Monday \n";
        break;
        case 4 : echo "Tuesday \n";
        break;
        case 5 : echo "Wednesday \n";
        break;
        case 6 : echo "Thursday \n";
        break;
        case 7 : echo "Friday \n";
        break;
    }
}
 
// Driver code
//date (dd/mm/yyyy)
Zellercongruence(22, 10, 2017);
 
// This code is contributed by ajit.
?>


Javascript




<script>
    // Javascript program to Find the Day for a Date
     
    // Print Day for a Date
    function Zellercongruence(day, month, year)
    {
        if (month == 1)
        {
            month = 13;
            year--;
        }
        if (month == 2)
        {
            month = 14;
            year--;
        }
        let q = day;
        let m = month;
        let k = year % 100;
        let j = parseInt(year / 100, 10);
        let h = q + parseInt(13 * (m + 1) / 5, 10) + k + parseInt(k / 4, 10) + parseInt(j / 4, 10) + 5 * j;
        h = h % 7;
        switch (h)
        {
            case 0 :
              document.write("Saturday");
              break;
                       
            case 1 :
                document.write("Sunday");
                break;
                       
            case 2 :
                document.write("Monday");
                break;
                       
            case 3 :
                document.write("Tuesday");
                break;
                       
            case 4 :
                document.write("Wednesday");
                break;
                       
            case 5 :
                document.write("Thursday");
                break;
                       
            case 6 :
                document.write("Friday");
                break;
        }
    }
     
    //date (dd/mm/yyyy)
      Zellercongruence(22, 10, 2017);
     
</script>


Output

Sunday 

Time Complexity: O(1)
Auxiliary Space: O(1)

Another variation of Zeller’s Congruence formula:

h=\left ( c/4 -2*c + y + y/4 + [\frac{13(m+1)}{5} ]+d-1 \right )mod7                    

C++




#include <bits/stdc++.h>
using namespace std;
string Zellercongruence(int day, int month, int year)
{
    vector<string> days
        = { "Sunday",   "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    int c = year / 100;
    year = year % 100;
    int h = (c / 4 - 2 * c + year + year / 4 + 13 * (month + 1) / 5 + day - 1) % 7;
    return days[(h + 7) % 7];
}
int main()
{
    cout << Zellercongruence(7, 3, 2003); // date (dd/mm/yyyy)
    return 0;
}
 
//This code is contributed by NarasingaNikhil


Python3




# code
def Zellercongruence(day, month, year):
    days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    if (month < 3):
        month += 12
        year -= 1
    c = year // 100
    year = year % 100
    h = (c // 4 - 2 * c + year + year // 4 + 13 * (month + 1) // 5 + day - 1) % 7
    return days[(h + 7) % 7]
 
 
print(Zellercongruence(7, 3, 2003))  # date (dd/mm/yyyy)


Javascript




function Zellercongruence(day, month, year) {
    let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    let c = Math.floor(year / 100);
    year = year % 100;
    let h = (c / 4 - 2 * c + year + year / 4 + 13 * (month + 1) / 5 + day - 1) % 7;
    return days[(h + 7) % 7];
}
 
console.log(Zellercongruence(7, 3, 2003)); // date (dd/mm/yyyy)


Java




import java.util.Arrays;
import java.util.List;
 
class GFG {
    public static String
    Zellercongruence(int day, int month, int year)
    {
        List<String> days = Arrays.asList(
            "Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday");
        if (month < 3) {
            month += 12;
            year -= 1;
        }
        int c = year / 100;
        year = year % 100;
        int h = (c / 4 - 2 * c + year + year / 4 + 13 * (month + 1) / 5 + day - 1) % 7;
        return days.get((h + 7) % 7);
    }
    public static void main(String[] args)
    {
        System.out.println(Zellercongruence(7, 3, 2003)); // date (dd/mm/yyyy)
    }
}


C#




using System;
 
class Program {
    static string Zellercongruence(int day, int month,
                                   int year)
    {
        string[] days
            = { "Sunday",    "Monday",   "Tuesday",
                "Wednesday", "Thursday", "Friday",
                "Saturday" };
        if (month < 3) {
            month += 12;
            year -= 1;
        }
        int c = year / 100;
        year = year % 100;
        int h = (c / 4 - 2 * c + year + year / 4
                 + 13 * (month + 1) / 5 + day - 1)
                % 7;
        return days[(h + 7) % 7];
    }
 
    static void Main(string[] args)
    {
        Console.WriteLine(Zellercongruence(
            7, 3, 2003)); // date (dd/mm/yyyy)
    }
}


Output

Friday

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials