# Find day of the week for a given date

Last Updated : 21 Oct, 2022

Write a function that calculates the day of the week for any particular date in the past or future. A typical application is to calculate the day of the week on which someone was born or some other special event occurred.

Recommended Practice

Following is a simple function suggested by Sakamoto, Lachman, Keith and Craver to calculate day. The following function returns 0 for Sunday, 1 for Monday, etc.

```Understanding the Maths:

14/09/1998
dd=14
mm=09
yyyy=1998 //non-leap year

Step 1: Information to be remembered.
Magic Number Month array.
For Year: {0,3,3,6,1,4,6,2,5,0,3,5}
DAY array starting from 0-6: {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
Century Year Value: 1600-1699 = 6
1700-1799 = 4
1800-1899 = 2
1900-1999 = 0
2000-2099 = 6..

Step 2: Calculations as per the steps

Last 2 digits of the year:  98
Divide the above by 4:     24
Take the date(dd):      14
Take month value from array: 5 (for September month number 9)
Take century year value:  0  ( 1998 is in the range 1900-1999 thus 0)
-----
Sum:        141

Divide the Sum by 7 and get the remainder: 141 % 7 = 1

Check the Day array starting from index 0: Day[1] = Monday

**If leap year it will be the remainder-1```

## C++

 `/* A program to find day of a given date */` `#include ` `using` `namespace` `std;`   `int` `dayofweek(``int` `d, ``int` `m, ``int` `y) ` `{ ` `    ``static` `int` `t[] = { 0, 3, 2, 5, 0, 3,` `                       ``5, 1, 4, 6, 2, 4 }; ` `    ``y -= m < 3;` `    ``return` `( y + y / 4 - y / 100 + ` `             ``y / 400 + t[m - 1] + d) % 7; ` `} `   `// Driver Code` `int` `main() ` `{ ` `    ``int` `day = dayofweek(30, 8, 2010); ` `    ``cout << day; `   `    ``return` `0; ` `} `   `// This is code is contributed ` `// by rathbhupendra`

## C

 `// C code to find day of a given date` `#include `   `int` `dayofweek(``int` `d, ``int` `m, ``int` `y)` `{` `    ``static` `int` `t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };` `    ``y -= m < 3;` `    ``return` `(y + y / 4 - y / 100 + y / 400 + t[m - 1] + d)` `           ``% 7;` `}`   `// Driver function to test above function` `int` `main()` `{` `    ``int` `day = dayofweek(30, 8, 2010);` `    ``printf``(``"%d"``, day);`   `    ``return` `0;` `}`

## Java

 `// Java code to implement the approach` `import` `java.util.*;`   `class` `FindDay {` `    ``static` `int` `dayofweek(``int` `d, ``int` `m, ``int` `y)` `    ``{` `        ``int` `t[] = { ``0``, ``3``, ``2``, ``5``, ``0``, ``3``, ``5``, ``1``, ``4``, ``6``, ``2``, ``4` `};` `        ``if` `(m < ``3``)` `            ``y--;` `        ``return` `(y + y / ``4` `- y / ``100` `+ y / ``400` `+ t[m - ``1``]` `                ``+ d)` `            ``% ``7``;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `day = dayofweek(``30``, ``8``, ``2010``);` `        ``System.out.println(day);` `    ``}` `}` `/*Contributed and written by Aniket Dey*/`

## Python3

 `# Python3 program to find day` `# of a given date`   `def` `dayofweek(d, m, y):` `    ``t ``=` `[ ``0``, ``3``, ``2``, ``5``, ``0``, ``3``,` `          ``5``, ``1``, ``4``, ``6``, ``2``, ``4` `]` `    ``y ``-``=` `m < ``3` `    ``return` `(( y ``+` `int``(y ``/` `4``) ``-` `int``(y ``/` `100``)` `             ``+` `int``(y ``/` `400``) ``+` `t[m ``-` `1``] ``+` `d) ``%` `7``)`   `# Driver Code` `day ``=` `dayofweek(``30``, ``8``, ``2010``)` `print``(day)`   `# This code is contributed by Shreyanshi Arun.`

## C#

 `// C# program to find day of a given date ` `using` `System;`   `class` `GFG {` `    `  `    ``static` `int` `dayofweek(``int` `d, ``int` `m, ``int` `y)` `    ``{` `        ``int` `[]t = { 0, 3, 2, 5, 0, 3, 5, ` `                            ``1, 4, 6, 2, 4 };` `        ``y -= (m < 3) ? 1 : 0;` `        `  `        ``return` `( y + y/4 - y/100 + y/400 ` `                         ``+ t[m-1] + d) % 7;` `    ``}` `    `  `    ``// Driver Program to test above function` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `day = dayofweek(30, 8, 2010);` `        `  `        ``Console.Write(day); ` `    ``}` `}`   `// This code is contributed by Sam007.`

## PHP

 ``

## Javascript

 ``

Output

`1`

Time Complexity: O(1)
Space Complexity: O(1), since no extra space has been taken.

See this for explanation of the above function.
References:
http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
This article is compiled by Dheeraj Jain and reviewed by GeeksforGeeks team.

Previous
Next
Share your thoughts in the comments