Talentd
FleetCode
Make and follow sheets.Sign in

Day of the Week solution and explanation

EasyMath5 min read59.6% acceptanceUpdated 9 Apr 2026
leetcode 1185 Day of the WeekCodebix · 13:00 · 6K viewsWatch on YouTube
9 more videos on this problem
  1. leetcode 1185 Day of the WeekCodebix · 13:00 · 6K views
  2. LeetCode 1185 | Day of the Week | Math | JavaSleepy Cracker · 7:38 · 1K views
  3. Vol63 - Leetcode - Problem 1185 - Day of the week - Golang - Phỏng vấn - Thuật toánYuh lập trình viên · 4:42 · 872 views
  4. 1185. Day of the Week | Python + Leetcode Technical Interview PracticeDC / AC · 14:59 · 640 views
  5. LeetCode Çözümleri - 1185. Day of the Week [Zeller's congruence]Soner Gönül · 8:29 · 301 views
  6. EP57 Coding Interview Question | Solution of Day of the Week in JavaScript | Leetcode 1185Rishabh1403 · 2:26 · 300 views
  7. L238, № 1185. Day of the Week / LeetcodeYersham · 5:21 · 174 views
  8. 1185. Day of The WeeklearnSimple Programming · 1:56:52 · 125 views
  9. No Sound Leetcode 1185: Day of the WeekExtremeProgrammer · 4:28 · 16 views
  10. LeetCode 1185: Day of the WeekAlitaCode · 24:43 · 9 views

The problem

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

Example 1:

Input: day = 31, month = 8, year = 2019
Output: "Saturday"

Example 2:

Input: day = 18, month = 7, year = 1999
Output: "Sunday"

Example 3:

Input: day = 15, month = 8, year = 1993
Output: "Sunday"

Constraints:

  • The given dates are valid dates between the years 1971 and 2100.

Approach overview

Problem Overview: Given a specific date (day, month, year), return the corresponding weekday such as Monday or Sunday. The challenge is converting a calendar date into a weekday index using either built-in date utilities or pure mathematical formulas.

Approach 1: Using Built-in Date Libraries (O(1) time, O(1) space)

The simplest solution relies on the standard date libraries available in most languages. You construct a date object using the provided day, month, and year, then call a function that returns the weekday index. Most libraries internally handle leap years, calendar transitions, and weekday calculation using optimized algorithms. After retrieving the weekday number, map it to the corresponding string (Sunday through Saturday). This approach is extremely reliable and concise, making it ideal in production code where correctness and readability matter more than implementing calendar math manually.

Approach 2: Implementing Zeller's Congruence (O(1) time, O(1) space)

Zeller's Congruence is a mathematical formula that directly computes the weekday for any Gregorian calendar date. The formula transforms the date into a modular arithmetic expression that returns an integer representing the day of the week. The key idea is adjusting months so that March becomes the first month of the year in the calculation, and January and February are treated as months 13 and 14 of the previous year. After computing the congruence value, apply a modulo operation to obtain a number between 0 and 6, then map it to the weekday name. This approach demonstrates deeper understanding of math and modular arithmetic, which interviewers often appreciate.

Zeller's formula works because it accounts for leap years and century corrections using integer division and modular arithmetic. The computation involves extracting the year of the century and the zero-based century, then combining them with the day and month offsets. Although the formula looks dense, it runs in constant time and requires only a few arithmetic operations.

Recommended for interviews: Implementing Zeller's Congruence usually makes a stronger impression because it shows you understand the underlying mathematical reasoning behind calendar calculations. The built-in library solution is perfectly valid and often expected in real-world systems, but the mathematical approach demonstrates algorithmic thinking and familiarity with classic date algorithms.

Approach 1: Using Built-in Libraries

Most programming languages have built-in libraries or modules that can handle date and time calculations. These libraries can directly provide functionalities to compute the day of the week for a given date, ensuring efficient and accurate results without manually implementing complex algorithms.

from datetime import datetime

def day_of_week(day, month, year):
    date = datetime(year, month, day)
    return date.strftime('%A')
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;

public class Solution {
    public static String dayOfWeek(int day, int month, int year) {
        LocalDate date = LocalDate.of(year, month, day);
        return date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
    }
}
#include <iostream>
#include <ctime>

std::string dayOfWeek(int day, int month, int year) {
    std::tm time_in = { 0, 0, 0, day, month - 1, year - 1900 };
    std::time_t time_temp = std::mktime(&time_in);
    const std::tm* time_out = std::localtime(&time_temp);
    const char* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    return days[time_out->tm_wday];
}
function dayOfWeek(day, month, year) {
    const date = new Date(year, month - 1, day);
    const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    return days[date.getUTCDay()];
}
#include <stdio.h>
#include <time.h>

const char* dayOfWeek(int day, int month, int year) {
    struct tm time_in = { 0, 0, 0, day, month - 1, year - 1900 };
    time_t time_temp = mktime(&time_in);
    struct tm* time_out = localtime(&time_temp);
    const char* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    return days[time_out->tm_wday];
}
using System;

public class Solution {
    public static string DayOfWeek(int day, int month, int year) {
        DateTime date = new DateTime(year, month, day);
        return date.DayOfWeek.ToString();
    }
}

This solution uses Python's datetime library. A date object is created using the provided year, month, and day. The strftime method with '%A' format code is used to get the full weekday name.

Complexity

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

Try this approach in the editor

Approach 2: Implementing Zeller's Congruence

Zeller's Congruence is a well-known algorithm that provides a formula to determine the day of the week for any given date. It accounts for leap years and the calendar adjustments made historically.

def zellers_congruence(day, month, year):
    if month < 3:
        month += 12
        year -= 1
    k = year % 100
    j = year // 100
    f = day + 13 * (month + 1) // 5 + k + k // 4 + j // 4 + 5 * j
    day_of_week = f % 7
    days = ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
    return days[day_of_week]
public class Zeller {
    public static String zellersCongruence(int day, int month, int year) {
        if (month < 3) {
            month += 12;
            year--;
        }
        int k = year % 100;
        int j = year / 100;
        int f = day + (13 * (month + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j;
        int dayOfWeek = f % 7;
        String[] days = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
        return days[dayOfWeek];
    }
}
#include <iostream>

std::string zellersCongruence(int day, int month, int year) {
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    int k = year % 100;
    int j = year / 100;
    int f = day + (13 * (month + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j;
    int dayOfWeek = f % 7;
    std::string days[] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    return days[dayOfWeek];
}
function zellersCongruence(day, month, year) {
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    const k = year % 100;
    const j = Math.floor(year / 100);
    const f = day + Math.floor(13 * (month + 1) / 5) + k + Math.floor(k / 4) + Math.floor(j / 4) + (5 * j);
    const dayOfWeek = f % 7;
    const days = ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
    return days[dayOfWeek];
}
#include <stdio.h>

const char* zellersCongruence(int day, int month, int year) {
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    int k = year % 100;
    int j = year / 100;
    int f = day + (13 * (month + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j;
    int dayOfWeek = f % 7;
    const char* days[] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    return days[dayOfWeek];
}
using System;

public class Zeller {
    public static string ZellersCongruence(int day, int month, int year) {
        if (month < 3) {
            month += 12;
            year--;
        }
        int k = year % 100;
        int j = year / 100;
        int f = day + (13 * (month + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j;
        int dayOfWeek = f % 7;
        string[] days = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
        return days[dayOfWeek];
    }
}

This Python solution implements Zeller's Congruence. The formula computes an integer that corresponds to a day of the week, which is mapped to a name in the days array.

Complexity

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

Try this approach in the editor

Approach 3: Zeller's Congruence

We can use Zeller's Congruence to calculate the day of the week. Zeller's Congruence is as follows:

$ w = (\left \lfloor \frac{c}{4} \right \rfloor - 2c + y + \left \lfloor \frac{y}{4} \right \rfloor + \left \lfloor \frac{13(m+1)}{5} \right \rfloor + d - 1) bmod 7

Where:

  • w: Day of the week (starting from Sunday)
  • c: First two digits of the year
  • y: Last two digits of the year
  • m: Month (the range of m is from 3 to 14, that is, in Zeller's Congruence, January and February of a certain year are considered as the 13th and 14th month of the previous year. For example, January 1, 2003 is considered as the 1st day of the 13th month of 2002)
  • d: Day
  • ⌊⌋: Floor function (round down)
  • mod: Modulo operation

The time complexity is O(1), and the space complexity is O(1)$.

function dayOfTheWeek(d: number, m: number, y: number): string {
    if (m < 3) {
        m += 12;
        y -= 1;
    }
    const c: number = (y / 100) | 0;
    y %= 100;
    const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
    const weeks: string[] = [
        'Sunday',
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
    ];
    return weeks[(w + 7) % 7];
}
func dayOfTheWeek(d int, m int, y int) string {
	if m < 3 {
		m += 12
		y -= 1
	}
	c := y / 100
	y %= 100
	w := (c/4 - 2*c + y + y/4 + 13*(m+1)/5 + d - 1) % 7
	weeks := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
	return weeks[(w+7)%7]
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Using Built-in Libraries

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

Implementing Zeller's Congruence

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

Zeller's Congruence–

Complexity analysis

ApproachTimeSpaceWhen to Use
Built-in Date LibrariesO(1)O(1)Best for production code where readability and reliability matter
Zeller's Congruence FormulaO(1)O(1)Preferred in interviews to demonstrate mathematical reasoning

Frequently asked questions

Is Day of the Week easy or hard?

Day of the Week is categorized as an Easy problem on LeetCode with an acceptance rate around 59%. The logic becomes straightforward when using built-in libraries, while implementing the mathematical formula adds a moderate conceptual challenge.

Day of the Week Python/Java solution

In Python, you can use the built-in datetime module and call weekday() or strftime to get the weekday name. In Java, LocalDate from java.time provides getDayOfWeek(). Both implementations compute the result in constant time.

How to solve Day of the Week in O(1)?

Use a direct calendar formula such as Zeller's Congruence. The formula converts the given day, month, and year into a weekday index using integer division and modular arithmetic. After computing the result modulo 7, map the number to the correct weekday string.

What is the best approach for Day of the Week?

The most practical approach uses built-in date libraries available in languages like Python, Java, and JavaScript. These libraries already implement optimized calendar algorithms and return the weekday in O(1) time. For interviews, implementing Zeller's Congruence is often preferred because it demonstrates understanding of calendar math and modular arithmetic.

Is Day of the Week asked at Google/Amazon/Meta?

Calendar and date calculation problems appear occasionally in interviews because they test attention to detail and mathematical reasoning. Variations of weekday calculation or date arithmetic have appeared in interviews at companies like Google and Amazon.

What data structure is used in Day of the Week?

No complex data structure is required. The solution relies mainly on arithmetic operations and a small array or list to map weekday indices (0–6) to names such as Sunday, Monday, and Tuesday.

What is the time complexity of Day of the Week?

Both common solutions run in O(1) time and O(1) space because the calculation involves only a fixed number of arithmetic operations or a single library call. No iteration or additional data structures are required.

Comments

Now write it yourselfClose this page and solve Day of the Week in the editor, without looking.
Practise Day of the Week