Finding day of week from date (day, month, year) in JavaScript

We are required to write a JavaScript function that takes in three arguments, namely: day, month and year. Based on these three inputs, our function should find the day of the week on that date.

For example: If the inputs are ?

day = 15,
month = 8,
year = 1993

Output

Then the output should be ?

const output = 'Sunday'

Using Built-in Date Object (Simple Method)

The easiest approach is to use JavaScript's built-in Date object:

function getDayOfWeek(day, month, year) {
    const date = new Date(year, month - 1, day); // month is 0-indexed
    const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    return days[date.getDay()];
}

console.log(getDayOfWeek(15, 8, 1993)); // Sunday
console.log(getDayOfWeek(1, 1, 2024));  // Monday
Sunday
Monday

Manual Calculation Method

For educational purposes, here's how to calculate the day manually by counting days from a reference point:

const dayOfTheWeek = (day, month, year) => {
    // JS months start at 0
    return dayOfTheWeekJS(day, month - 1, year);
}

function dayOfTheWeekJS(day, month, year) {
    const DAYS = [
        'Sunday',
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
    ];
    const DAY_1970_01_01 = 4; // January 1, 1970 was Thursday (index 4)
    let days = day - 1;
    
    // Add days from previous months
    while (month - 1 >= 0) {
        days += daysInMonthJS(month - 1, year);
        month -= 1;
    }
    
    // Add days from previous years
    while (year - 1 >= 1970) {
        days += daysInYear(year - 1);
        year -= 1;
    }
    
    return DAYS[(days + DAY_1970_01_01) % DAYS.length];
}

function daysInMonthJS(month, year) {
    const days = [
        31, // January
        28 + (isLeapYear(year) ? 1 : 0), // February
        31, // March
        30, // April
        31, // May
        30, // June
        31, // July
        31, // August
        30, // September
        31, // October
        30, // November
        31, // December
    ];
    return days[month];
}

function daysInYear(year) {
    return 365 + (isLeapYear(year) ? 1 : 0);
}

function isLeapYear(year) {
    return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
}

console.log(dayOfTheWeek(15, 8, 1993));
Sunday

Explanation

The manual calculation method works by counting total days from Unix epoch (January 1, 1970, which was a Thursday). The algorithm:

  • Count days in complete years since 1970

  • Count days in complete months for the current year

  • Add remaining days for the incomplete month

  • Use modulo 7 to find the corresponding day of the week

Comparison

Method Complexity Performance Use Case
Date Object Simple Fast Production code
Manual Calculation Complex Slower Learning algorithms

Conclusion

For practical applications, use JavaScript's built-in Date object with getDay(). The manual calculation approach is useful for understanding date algorithms and works without relying on built-in functions.

Updated on: 2026-03-15T23:19:00+05:30

800 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements