Find day of the week for a given date (original) (raw)

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:

#include 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

m,m, 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=(yearCode = (yearCode=(y % 100) + floor($y % 100 / 4); // Adjust year code for the century yearCode=(yearCode = (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[monthCode[monthCode[m - 1] + $yearCode) % 7; } // Input: day, month, and year day=15;day = 15; day=15;month = 6; $year = 1995; // Calculate the day of the week using the formula-based approach dayOfWeekResult=dayOfWeek(dayOfWeekResult = dayOfWeek(dayOfWeekResult=dayOfWeek(day, month,month, month,year); // Output the result as an integer (0 = Sunday, 1 = Monday, ..., 6 = Saturday) echo $dayOfWeekResult; ?>

`

**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.

/* 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

m,m, m,y) { static $t = array(0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4); y−=y -= y=m < 3; return ($y + y/4−y / 4 - y/4y / 100 + y/400+y / 400 + y/400+t[$m - 1] + $d) % 7; } // Driver Code $day = dayofweek(30, 8, 2010); echo $day; ?>

`

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