Day of the Week

Medium~20 min

Given a date as three integers — year, month, and day — return the day of the week as a string: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", or "Saturday".

The date will always be a valid Gregorian calendar date with year >= 1583.

You may use Zeller's congruence or any other algorithm to compute the day. Zeller's congruence works as follows:

  1. If month < 3, treat it as month 13 or 14 of the previous year (i.e., add 12 to month, subtract 1 from year)
  2. Compute: h = (day + floor(13*(month+1)/5) + k + floor(k/4) + floor(j/4) - 2*j) mod 7
    • Where k = year % 100 and j = floor(year / 100)
  3. h maps to: 0=Saturday, 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday

Note: Some languages produce negative remainders for the modulo operation. Make sure h is non-negative (e.g., use ((value % 7) + 7) % 7).

Examples

Example 1
Input: year = 2024, month = 1, day = 1
Output: "Monday"
Explanation: January 1, 2024 is a Monday.
Example 2
Input: year = 2023, month = 12, day = 25
Output: "Monday"
Explanation: December 25, 2023 (Christmas) is a Monday.
Example 3
Input: year = 2000, month = 2, day = 29
Output: "Tuesday"
Explanation: 2000 is a leap year. February 29, 2000 is a Tuesday.

Constraints

  • 1583 <= year <= 9999
  • 1 <= month <= 12
  • 1 <= day <= 31
  • The date is always valid in the Gregorian calendar
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run