Find day of the week for a given date
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.
Following is a simple C function suggested by Sakamoto, Lachman, Keith and Craver to calculate day. The following function returns 0 for Sunday, 1 for Monday, etc.
C
/* A program to find day of a given date */#include<stdio.h> 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; } |
chevron_right
filter_none
Java
// A program to find day of a given date 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(String arg[]) { int day = dayofweek(30, 8, 2010); System.out.print(day); } } // This code is contributed // by Anant Agarwal. |
chevron_right
filter_none
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. |
chevron_right
filter_none
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. |
chevron_right
filter_none
PHP
<?php // PHP program to find // day of a given date function dayofweek($d, $m, $y) { static $t = array(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 $day = dayofweek(30, 8, 2010); echo $day; // This code is contributed by mits. ?> |
chevron_right
filter_none
Output : 1 (Monday)
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. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Python program to find day of the week for a given date
- Date after adding given number of days to the given date
- Zeller's Congruence | Find the Day for a Date
- PHP | Number of week days between two dates
- Tomohiko Sakamoto's Algorithm- Finding the day of the week
- Print day of week using Gregorian Calendar in java
- Date.UTC( ) In JavaScript
- Convert String to Date in Java
- Date class in Java (With Examples)
- PHP | Converting string to Date and DateTime
- Program to check if a date is valid or not
- Java | Current date and time
- Convert timestamp to readable date/time in PHP
- Java | Date format validation using Regex
- C++ Program to print current Day, Date and Time



