Open In App

Find day of the week for a given date

Last Updated : 14 Feb, 2025
Comments
Improve
Suggest changes
38 Likes
Like
Report

Given a date (day, month, year), the task is to determine the day of the week on which that date falls. The function should be able to compute the day for any date in the past or future. The function should return values from 0 to 6 where 0 means Sunday, 1 Monday and so on.

Examples:

Input: d = 30, m = 8, y = 2010
Output: 1
Explanation: 30th August 2010 was a Monday.

Input: d = 15, m = 6, y = 1995
Output: 4
Explanation: 15th June 1995 was a Thursday.

Input: d = 29, m = 2, y = 2016
Output: 1
Explanation: 26th January was a Monday.

Formula-based Approach

The Formula-Based Approach for calculating the day of the week is an efficient method that calculates the day using simple arithmetic operations. It leverages precomputed month codes and year codes to determine the day on a given date.

Note: This approach doesn't work for leap year days.

Formula:

dayOfWeek = (d + monthCode + yearCode) % 7

Steps for Calculation:

  • Step 1: Calculate the Month Code: Each month has a predefined month code, and the value for each month is as follows:
    • January = 6, February = 2 (1 for leap years), March = 2, April = 5, May = 0, June = 3, July = 5, August = 1, September = 4, October = 6, November = 2, December = 4
  • Step 2: Calculate the Year Code: The year code is calculated based on the last two digits of the year. The steps are:
    • Take the last two digits of the year. For 1995, the last two digits are 95.
    • Multiply by 1.25: 95×1.25=118.75
    • Take the floor of the result (i.e., the greatest integer less than or equal to the result)
    • Apply modulo 7: 118%7=5
    • If the year is a multiple of 400, use the last two digits as they are without any changes.
  • Step 3: Apply Century Adjustment: Adjust for the century of the year:
    • For years between 100 and 199: Add 5.
    • For years between 200 and 299: Add 3.
    • For years between 300 and 399: Add 1.
    • This step is used to account for the shifting of days due to century years (e.g., 1800, 1900).
  • Step 4: Calculate the Day of the Week
    • Now that we have the date, month code, and year code, we simply sum them and take the result modulo 7 to determine the day of the week:
C++
#include <iostream>
using namespace std;

// Function to calculate the day of the week using the formula-based approach
int dayOfWeek(int d, int m, int y) {
    // Predefined month codes for each month
    static int monthCode[] = {6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

    // Adjust year for January and February
    if (m < 3) {
        y -= 1;  // If month is January or February, treat them as part of the previous year
    }

    // Calculate the year code
    int yearCode = (y % 100) + (y % 100) / 4;

    // Adjust year code for the century
    yearCode = (yearCode + (y / 100) / 4 + 5 * (y / 100)) % 7;

    // Calculate the day of the week and return the value as an integer
    return (d + monthCode[m - 1] + yearCode) % 7;
}

int main() {
    // Input: day, month, and year
    int day = 15, month = 6, year = 1995;

    // Calculate the day of the week using the formula-based approach
    int dayOfWeekResult = dayOfWeek(day, month, year);

    // Output the result as an integer (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
    cout << dayOfWeekResult << endl;

    return 0;
}
C
#include <stdio.h>

// Function to calculate the day of the week using the formula-based approach
int dayOfWeek(int d, int m, int y) {
    // Predefined month codes for each month
    static int monthCode[] = {6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

    // Adjust year for January and February
    if (m < 3) {
        y -= 1;  // If month is January or February, treat them as part of the previous year
    }

    // Calculate the year code
    int yearCode = (y % 100) + (y % 100) / 4;

    // Adjust year code for the century
    yearCode = (yearCode + (y / 100) / 4 + 5 * (y / 100)) % 7;

    // Calculate the day of the week and return the value as an integer
    return (d + monthCode[m - 1] + yearCode) % 7;
}

int main() {
    // Input: day, month, and year
    int day = 15, month = 6, year = 1995;

    // Calculate the day of the week using the formula-based approach
    int dayOfWeekResult = dayOfWeek(day, month, year);

    // Output the result as an integer (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
    printf("%d\n", dayOfWeekResult);

    return 0;
}
Java
public class DayOfWeek {
    // Function to calculate the day of the week using the formula-based approach
    public static int dayOfWeek(int d, int m, int y) {
        // Predefined month codes for each month
        int[] monthCode = {6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

        // Adjust year for January and February
        if (m < 3) {
            y -= 1;  // If month is January or February, treat them as part of the previous year
        }

        // Calculate the year code
        int yearCode = (y % 100) + (y % 100) / 4;

        // Adjust year code for the century
        yearCode = (yearCode + (y / 100) / 4 + 5 * (y / 100)) % 7;

        // Calculate the day of the week and return the value as an integer
        return (d + monthCode[m - 1] + yearCode) % 7;
    }

    public static void main(String[] args) {
        // Input: day, month, and year
        int day = 15, month = 6, year = 1995;

        // Calculate the day of the week using the formula-based approach
        int dayOfWeekResult = dayOfWeek(day, month, year);

        // Output the result as an integer (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
        System.out.println(dayOfWeekResult);
    }
}
Python
def day_of_week(d, m, y):
    # Predefined month codes for each month
    month_code = [6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]

    # Adjust year for January and February
    if m < 3:
        y -= 1  # If month is January or February, treat them as part of the previous year

    # Calculate the year code
    year_code = (y % 100) + (y % 100) // 4

    # Adjust year code for the century
    year_code = (year_code + (y // 100) // 4 + 5 * (y // 100)) % 7

    # Calculate the day of the week and return the value as an integer
    return (d + month_code[m - 1] + year_code) % 7

# Input: day, month, and year
day = 15
month = 6
year = 1995

# Calculate the day of the week using the formula-based approach
day_of_week_result = day_of_week(day, month, year)

# Output the result as an integer (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
print(day_of_week_result)
JavaScript
// Function to calculate the day of the week using the formula-based approach
function dayOfWeek(d, m, y) {
    // Predefined month codes for each month
    const monthCode = [6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];

    // Adjust year for January and February
    if (m < 3) {
        y -= 1;  // If month is January or February, treat them as part of the previous year
    }

    // Calculate the year code
    let yearCode = (y % 100) + Math.floor(y % 100 / 4);

    // Adjust year code for the century
    yearCode = (yearCode + Math.floor(y / 100) / 4 + 5 * Math.floor(y / 100)) % 7;

    // Calculate the day of the week and return the value as an integer
    return (d + monthCode[m - 1] + yearCode) % 7;
}

// Input: day, month, and year
let day = 15, month = 6, year = 1995;

// Calculate the day of the week using the formula-based approach
let dayOfWeekResult = dayOfWeek(day, month, year);

// Output the result as an integer (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
console.log(dayOfWeekResult);
PHP
<?php
// Function to calculate the day of the week using the formula-based approach
function dayOfWeek($d, $m, $y) {
    // Predefined month codes for each month
    $monthCode = [6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];

    // Adjust year for January and February
    if ($m < 3) {
        $y -= 1;  // If month is January or February, treat them as part of the previous year
    }

    // Calculate the year code
    $yearCode = ($y % 100) + floor($y % 100 / 4);

    // Adjust year code for the century
    $yearCode = ($yearCode + floor($y / 100) / 4 + 5 * floor($y / 100)) % 7;

    // Calculate the day of the week and return the value as an integer
    return ($d + $monthCode[$m - 1] + $yearCode) % 7;
}

// Input: day, month, and year
$day = 15; $month = 6; $year = 1995;

// Calculate the day of the week using the formula-based approach
$dayOfWeekResult = dayOfWeek($day, $month, $year);

// Output the result as an integer (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
echo $dayOfWeekResult;
?>

Output
4

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

Zeller's Congruence-Based Approach

This approach calculates the day of the week for a given date using a formula involving the year, month, and day. It is essentially a simplified version of Zeller's Congruence, with adjustments for century years and leap years.

  • Predefined Month Codes (t[]):
    • The array t[] stores month codes for each month. These values are predefined to account for the different lengths of each month.
    • For example: January = 6, February = 2 (or 1 for leap years), March = 2, etc.
  • Adjusting the Year for January and February (y -= m < 3):
    • The code adjusts the year if the month is January or February.
    • This is because in Zeller's Congruence, January and February are treated as the 13th and 14th months of the previous year.
    • If the month is January or February, it reduces the year by 1 (i.e., y -= 1).
  • Leap Year Adjustments (y / 4 - y / 100 + y / 400): The formula adjusts for leap years and century years:
    • y / 4: Adds an adjustment for the leap years.
    • y / 100: Subtracts the days for century years that are not leap years.
    • y / 400: Adds back the leap days for century years that are divisible by 400 (e.g., 1600, 2000).
  • Final Calculation:
    • The formula: (y + y/4 − y/100 + y/400 + t[m−1] + d) % 7 calculates the day of the week by summing the day (d), month code (t[m - 1]), and adjusted year values.
    • The result is then taken modulo 7, which gives a value between 0 and 6, corresponding to the days of the week: 0 = Sunday, 1 = Monday, ..., 6 = Saturday.
C++
/* A program to find day of a given date */
#include <bits/stdc++.h>
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; 
} 
C
// C code 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;
}
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);
    }
}
Python
# 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)
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); 
    }
}
JavaScript
// Javascript program to find day of a given date

function dayofweek(d, m, y)
{
    let 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 Code

    let day = dayofweek(30, 8, 2010);
    console.log(Math.round(day));
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;

?>

Output
1

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



Explore