Isaac Emmanuel
09 Sept 2026•EasyGo / Python / TypeScript

Day of the Week

Return the weekday name for a Gregorian calendar date.

Count elapsed days from 1 January 1971, adding leap days explicitly, and offset the result from that date's known Friday.

Complexity

O(year - 1971) time and O(1) space.

Solution files

Go•day-of-the-week/solution.go
package main

func dayOfTheWeek(day int, month int, year int) string {
	leap := func(value int) bool { return value%400 == 0 || value%4 == 0 && value%100 != 0 }
	monthDays := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
	elapsed := day - 1
	for value := 1971; value < year; value++ {
		if leap(value) {
			elapsed += 366
		} else {
			elapsed += 365
		}
	}
	for index := 1; index < month; index++ {
		elapsed += monthDays[index-1]
		if index == 2 && leap(year) {
			elapsed++
		}
	}
	return []string{"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"}[elapsed%7]
}
Python•day-of-the-week/solution.py
class Solution:
    def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
        leap = lambda value: value % 400 == 0 or value % 4 == 0 and value % 100 != 0
        month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        elapsed = day - 1 + sum(366 if leap(value) else 365 for value in range(1971, year))
        elapsed += sum(month_days[index - 1] + int(index == 2 and leap(year)) for index in range(1, month))
        return ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"][elapsed % 7]
TypeScript•day-of-the-week/solution.ts
function dayOfTheWeek(day: number, month: number, year: number): string {
  const leap = (value: number): boolean => value % 400 === 0 || (value % 4 === 0 && value % 100 !== 0); const monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; let elapsed = day - 1;
  for (let value = 1971; value < year; value += 1) elapsed += leap(value) ? 366 : 365; for (let index = 1; index < month; index += 1) elapsed += monthDays[index - 1] + Number(index === 2 && leap(year));
  return ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"][elapsed % 7];
}