Day of the Week
EasyUpdated: Aug 2, 2025
Practice on:
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"}.
Examples
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
1971and2100.
Solution
Method 1 – Zeller's Congruence 1
Intuition
We can use Zeller's Congruence, a well-known formula to compute the day of the week for any given date. It works by transforming the date into a formula that outputs the weekday as an integer, which we can map to the required weekday names.
Approach
- If the month is January or February, treat it as month 13 or 14 of the previous year.
- Apply Zeller's Congruence formula to compute the weekday index.
- Map the index to the correct weekday name as per the problem's requirements.
- Return the weekday name.
Code
C++
class Solution {
public:
string dayOfTheWeek(int d, int m, int y) {
vector<string> wd = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
if (m < 3) {
m += 12;
y--;
}
int k = y % 100;
int j = y / 100;
int h = (d + 13 * (m + 1) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;
// Zeller's: 0=Saturday, 1=Sunday, ...
return wd[(h + 6) % 7];
}
};
Go
func dayOfTheWeek(d, m, y int) string {
wd := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
if m < 3 {
m += 12
y--
}
k := y % 100
j := y / 100
h := (d + 13*(m+1)/5 + k + k/4 + j/4 + 5*j) % 7
return wd[(h+6)%7]
}
Java
class Solution {
public String dayOfTheWeek(int d, int m, int y) {
String[] wd = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
if (m < 3) {
m += 12;
y--;
}
int k = y % 100;
int j = y / 100;
int h = (d + 13 * (m + 1) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;
return wd[(h + 6) % 7];
}
}
Kotlin
class Solution {
fun dayOfTheWeek(d: Int, m: Int, y: Int): String {
val wd = listOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
var mm = m
var yy = y
if (mm < 3) {
mm += 12
yy--
}
val k = yy % 100
val j = yy / 100
val h = (d + 13 * (mm + 1) / 5 + k + k / 4 + j / 4 + 5 * j) % 7
return wd[(h + 6) % 7]
}
}
Python
class Solution:
def dayOfTheWeek(self, d: int, m: int, y: int) -> str:
wd = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
if m < 3:
m += 12
y -= 1
k = y % 100
j = y // 100
h = (d + 13 * (m + 1) // 5 + k + k // 4 + j // 4 + 5 * j) % 7
return wd[(h + 6) % 7]
Rust
impl Solution {
pub fn day_of_the_week(d: i32, m: i32, y: i32) -> String {
let wd = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let (mut m, mut y) = (m, y);
if m < 3 {
m += 12;
y -= 1;
}
let k = y % 100;
let j = y / 100;
let h = (d + (13 * (m + 1)) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;
wd[((h + 6) % 7) as usize].to_string()
}
}
TypeScript
class Solution {
dayOfTheWeek(d: number, m: number, y: number): string {
const wd = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
if (m < 3) {
m += 12;
y--;
}
const k = y % 100;
const j = Math.floor(y / 100);
const h = (d + Math.floor(13 * (m + 1) / 5) + k + Math.floor(k / 4) + Math.floor(j / 4) + 5 * j) % 7;
return wd[(h + 6) % 7];
}
}
Complexity
- ⏰ Time complexity:
O(1), as all operations are arithmetic and do not depend on input size. - 🧺 Space complexity:
O(1), as only a few variables are used.