Posted on by Kalkicode
Code Mathematics

Find day of the week for a given date

The problem of finding the day of the week for a given date is a common task in computer programming and mathematics. The goal is to determine which day of the week (e.g. Monday, Tuesday, Wednesday, etc.) a specific date falls on, given the month, day, and year.

There are several ways to find the day of the week for a given date, including the following methods:

Zeller's Congruence:

Zeller's congruence is a mathematical formula that can be used to calculate the day of the week for any date in the Gregorian calendar. The formula is as follows:

h = (q + ((13*(m+1))/5) + K + (K/4) + (J/4) - (2*J)) % 7

Where:

  • h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, etc.)
  • q is the day of the month
  • m is the month (3 = March, 4 = April, ..., 14 = February)
  • K is the year of the century (i.e. year % 100)
  • J is the zero-based century (i.e. year // 100)

Note that for January and February, you need to add 12 to the month and subtract 1 from the year.

Tomohiko Sakamoto's Algorithm:

The Sakamoto's algorithm is a simpler alternative to Zeller's congruence. The algorithm is as follows:

t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
y -= m < 3
dow = (y + y//4 - y//100 + y//400 + t[m-1] + d) % 7

Where:

  • dow is the day of the week (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.)
  • d is the day of the month
  • m is the month (1 = January, 2 = February, ..., 12 = December)
  • y is the year

Note that the algorithm assumes the Gregorian calendar, so it may not be accurate for dates before 1582.

Program Solution

// C program for 
// Find day of the week for a given date
#include <stdio.h>

void dayName(int year, int month, int day)
{
	if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0)
	{
		return;
	}
	// Day names
	const char *result[] = {
		"Sunday" , "Monday" , "Tuesday" , "Wednesday" , 
        "Thursday" , "Friday" , "Saturday"
	};
	// Month  1   2   3   4   5   6   7   8   9   10  11  12
	int m[] = {
		0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4
	};
	// Calculate day of week
	int d = (year + year / 4 - year / 100 + year / 400 + m[month - 1] + day) % 7;
	// Display given date 
	printf(" Y:%d  M:%d  D:%d \n", year, month, day);
	// Display calculated result
	printf(" Week Day : %s \n", result[d]);
}
int main(int argc, char const *argv[])
{
	// Test case
	dayName(2020, 11, 15);
	dayName(2021, 6, 24);
	return 0;
}

input

 Y:2020  M:11  D:15
 Week Day : Sunday
 Y:2021  M:6  D:24
 Week Day : Thursday
/*
  Java Program for
  Find day of the week for a given date
*/
public class Calculation
{
	public void dayName(int year, int month, int day)
	{
		if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0)
		{
			return;
		}
		// Day names
		String[] result = {
			"Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday"
		};
		// Month 
		// 1   2   3   4   5   6   7   8   9   10  11  12
		int[] m = {
			0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4
		};
		// Calculate day of week
		int d = (year + year / 4 - year / 100 + year / 400 + m[month - 1] + day) % 7;
		// Display given date 
		System.out.println(" Y:" + year + " M:" + month + " D:" + day);
		// Display calculated result
		System.out.println(" Week Day : " + result[d]);
	}
	public static void main(String[] args)
	{
		Calculation task = new Calculation();
		// Test case
		task.dayName(2020, 11, 15);
		task.dayName(2021, 6, 24);
	}
}

input

 Y:2020 M:11 D:15
 Week Day : Sunday
 Y:2021 M:6 D:24
 Week Day : Thursday
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Find day of the week for a given date
*/
class Calculation
{
	public: void dayName(int year, int month, int day)
	{
		if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0)
		{
			return;
		}
		// Day names
		string result[] = {
			"Sunday" , "Monday" , "Tuesday" , "Wednesday" , 
            "Thursday" , "Friday" , "Saturday"
		};
		// Month 
		// 1   2   3   4   5   6   7   8   9   10  11  12
		int m[] = {
			0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4
		};
		// Calculate day of week
		int d = (year + year / 4 - year / 100 + year / 400 + m[month - 1] + day) % 7;
		// Display given date 
		cout << " Y:" << year << " M:" << month << " D:" << day << endl;
		// Display calculated result
		cout << " Week Day : " << result[d] << endl;
	}
};
int main()
{
	Calculation *task = new Calculation();
	// Test case
	task->dayName(2020, 11, 15);
	task->dayName(2021, 6, 24);
	return 0;
}

input

 Y:2020 M:11 D:15
 Week Day : Sunday
 Y:2021 M:6 D:24
 Week Day : Thursday
// Include namespace system
using System;
/*
  Csharp Program for
  Find day of the week for a given date
*/
public class Calculation
{
	public void dayName(int year, int month, int day)
	{
		if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0)
		{
			return;
		}
		// Day names
		String[] result = {
			"Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday"
		};
		// Month 
		// 1   2   3   4   5   6   7   8   9   10  11  12
		int[] m = {
			0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4
		};
		// Calculate day of week
		int d = (year + year / 4 - year / 100 + year / 400 + m[month - 1] + day) % 7;
		// Display given date 
		Console.WriteLine(" Y:" + year + " M:" + month + " D:" + day);
		// Display calculated result
		Console.WriteLine(" Week Day : " + result[d]);
	}
	public static void Main(String[] args)
	{
		Calculation task = new Calculation();
		// Test case
		task.dayName(2020, 11, 15);
		task.dayName(2021, 6, 24);
	}
}

input

 Y:2020 M:11 D:15
 Week Day : Sunday
 Y:2021 M:6 D:24
 Week Day : Thursday
<?php
/*
  Php Program for
  Find day of the week for a given date
*/
class Calculation
{
	public	function dayName($year, $month, $day)
	{
		if ($month < 1 || $month > 12 || $day < 1 || $day > 31 || $year <= 0)
		{
			return;
		}
		// Day names
		$result = array("Sunday", "Monday", "Tuesday", 
                        "Wednesday", "Thursday", "Friday", "Saturday");
		// Month 
		// 1   2   3   4   5   6   7   8   9   10  11  12
		$m = array(0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4);
		// Calculate day of week
		$d = ($year + (int)($year / 4) - (int)($year / 100) + 
              (int)($year / 400) + $m[$month - 1] + $day) % 7;
		// Display given date 
		echo " Y:".$year.
		" M:".$month.
		" D:".$day.
		"\n";
		// Display calculated result
		echo " Week Day : ".$result[$d].
		"\n";
	}
}

function main()
{
	$task = new Calculation();
	// Test case
	$task->dayName(2020, 11, 15);
	$task->dayName(2021, 6, 24);
}
main();

input

 Y:2020 M:11 D:15
 Week Day : Sunday
 Y:2021 M:6 D:24
 Week Day : Thursday
/*
  Node JS Program for
  Find day of the week for a given date
*/
class Calculation
{
	dayName(year, month, day)
	{
		if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0)
		{
			return;
		}
		// Day names
		var result = ["Sunday", "Monday", "Tuesday", 
                      "Wednesday", "Thursday", "Friday", "Saturday"];
		// Month 
		// 1   2   3   4   5   6   7   8   9   10  11  12
		var m = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
		// Calculate day of week
		var d = (year + parseInt(year / 4) - parseInt(year / 100) + 
                 parseInt(year / 400) + m[month - 1] + day) % 7;
		// Display given date 
		console.log(" Y:" + year + " M:" + month + " D:" + day);
		// Display calculated result
		console.log(" Week Day : " + result[d]);
	}
}

function main()
{
	var task = new Calculation();
	// Test case
	task.dayName(2020, 11, 15);
	task.dayName(2021, 6, 24);
}
main();

input

 Y:2020 M:11 D:15
 Week Day : Sunday
 Y:2021 M:6 D:24
 Week Day : Thursday
#  Python 3 Program for
#  Find day of the week for a given date
class Calculation :
	def dayName(self, year, month, day) :
		if (month < 1 or month > 12 or day < 1 or day > 31 or year <= 0) :
			return
		
		result = ["Sunday", "Monday", "Tuesday", "Wednesday", 
                  "Thursday", "Friday", "Saturday"]
		m = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
		d = (year + int(year / 4) - int(year / 100) + 
             int(year / 400) + m[month - 1] + day) % 7
		#  Display given date 
		print(" Y:", year ," M:", month ," D:", day)
		#  Display calculated result
		print(" Week Day : ", result[d])
	

def main() :
	task = Calculation()
	#  Test case
	task.dayName(2020, 11, 15)
	task.dayName(2021, 6, 24)

if __name__ == "__main__": main()

input

 Y: 2020  M: 11  D: 15
 Week Day :  Sunday
 Y: 2021  M: 6  D: 24
 Week Day :  Thursday
#  Ruby Program for
#  Find day of the week for a given date

class Calculation 
	def dayName(year, month, day) 
		if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0) 
			return
		end

		#  Day names
		result = ["Sunday", "Monday", "Tuesday", 
                  "Wednesday", "Thursday", "Friday", "Saturday"]
		#  Month 
		#  1   2   3   4   5   6   7   8   9   10  11  12
		m = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
		#  Calculate day of week
		d = (year + year / 4 - year / 100 + year / 400 + 
             m[month - 1] + day) % 7
		#  Display given date 
		print(" Y:", year ," M:", month ," D:", day, "\n")
		#  Display calculated result
		print(" Week Day : ", result[d], "\n")
	end

end

def main() 
	task = Calculation.new()
	#  Test case
	task.dayName(2020, 11, 15)
	task.dayName(2021, 6, 24)
end

main()

input

 Y:2020 M:11 D:15
 Week Day : Sunday
 Y:2021 M:6 D:24
 Week Day : Thursday
/*
  Scala Program for
  Find day of the week for a given date
*/
class Calculation()
{
	def dayName(year: Int, month: Int, day: Int): Unit = {
		if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0)
		{
			return;
		}
		// Day names
		var result: Array[String] = Array("Sunday", "Monday", 
                                          "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
		// Month 
		// 1   2   3   4   5   6   7   8   9   10  11  12
		var m: Array[Int] = Array(0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4);
		// Calculate day of week
		var d: Int = (year + (year / 4).toInt - (year / 100).toInt + 
                      (year / 400).toInt + m(month - 1) + day) % 7;
		// Display given date 
		println(" Y:" + year + " M:" + month + " D:" + day);
		// Display calculated result
		println(" Week Day : " + result(d));
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Calculation = new Calculation();
		// Test case
		task.dayName(2020, 11, 15);
		task.dayName(2021, 6, 24);
	}
}

input

 Y:2020 M:11 D:15
 Week Day : Sunday
 Y:2021 M:6 D:24
 Week Day : Thursday
/*
  Swift 4 Program for
  Find day of the week for a given date
*/
class Calculation
{
	func dayName(_ year: Int, _ month: Int, _ day: Int)
	{
		if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0)
		{
			return;
		}
		// Day names
		let result: [String] = ["Sunday", "Monday", "Tuesday", 
                                "Wednesday", "Thursday", "Friday", "Saturday"];
		// Month 
		// 1   2   3   4   5   6   7   8   9   10  11  12
		let m: [Int] = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
		// Calculate day of week
		let d: Int = (year + year / 4 - year / 100 + year / 400 + 
                      m[month - 1] + day) % 7;
		// Display given date 
		print(" Y:", year ," M:", month ," D:", day);
		// Display calculated result
		print(" Week Day : ", result[d]);
	}
}
func main()
{
	let task: Calculation = Calculation();
	// Test case
	task.dayName(2020, 11, 15);
	task.dayName(2021, 6, 24);
}
main();

input

 Y: 2020  M: 11  D: 15
 Week Day :  Sunday
 Y: 2021  M: 6  D: 24
 Week Day :  Thursday
/*
  Kotlin Program for
  Find day of the week for a given date
*/
class Calculation
{
	fun dayName(year: Int, month: Int, day: Int): Unit
	{
		if (month < 1 || month > 12 || day < 1 || day > 31 || year <= 0)
		{
			return;
		}
		// Day names
		val result: Array < String > = arrayOf("Sunday", "Monday", "Tuesday",
                                               "Wednesday", "Thursday", "Friday", 
                                               "Saturday");
		// Month 
		// 1   2   3   4   5   6   7   8   9   10  11  12
		val m: Array < Int > = arrayOf(0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4);
		// Calculate day of week
		val d: Int = (year + year / 4 - year / 100 + year / 400 + 
                      m[month - 1] + day) % 7;
		// Display given date 
		println(" Y:" + year + " M:" + month + " D:" + day);
		// Display calculated result
		println(" Week Day : " + result[d]);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Calculation = Calculation();
	// Test case
	task.dayName(2020, 11, 15);
	task.dayName(2021, 6, 24);
}

input

 Y:2020 M:11 D:15
 Week Day : Sunday
 Y:2021 M:6 D:24
 Week Day : Thursday

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment