skip to Main Content

Given that 18 May 2024 is a Saturday, how do you determine which day 1 May falls on without using new Date()?

Edit:
The answer I provided should work for all the programming languages that has index 0 as Sunday. Since I couldn’t find the answer in SO, I decided to craft this answer out so that it help future developers that may ask the following question.

The reason why I added "without new Date()" is to prevent SO users from answering new Date(2024, 4, 1).getDay() which add a layer of process to handle. Other than the following answer, if there is a better algorithm, feel free to provide so that we can contribute to SO.

2

Answers


  1. Chosen as BEST ANSWER
    const cur_date = 18
    const cur_day = 6 // 0 - Sun, ..., 6 - Sat
    const days = ['sun', 'mon', 'tue', 'wed', 'thur', 'fri', 'sat']
    const res = cur_day - cur_date % 7 + 1 // Number can be reset by +7 if value is negative.
    console.log(days.at(res))
    

    When you modulo the cur_date by 7, it will return the result to the earliest date that falls on Saturday which is 4. Afterwards, you deduct from the day itself (6 - 4) which will return 2 (Tuesday). To offset that, you will need to + 1.

    Below is an example of the May 2024 Calendar.

    [SUN][MON][TUE][WED][THU][FRI][SAT]
    [xx] [xx] [xx] [01] [02] [03] [04]
    [05] [06] [07] [08] [09] [10] [11]
    [12] [13] [14] [15] [16] [17] [18]
    [19] [20] [21] [22] [23] [24] [25]
    [26] [27] [28] [29] [30] [31] [xx]
    

  2. We know that there are 7 days:

    let days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    

    we also know that May 18th is a Saturday, that is:

    days[days.indexOf('Saturday')] === 5
    

    and we also know that May 18 – May 1 = 17 days

    (5 - 17) % 7 === -5
    

    which is in the modulo class of 2, because -5 + 7 === 2, therefore, May 1st was on days[2] which was a Wednesday.

    But when will future May 1st dates will occur? If we ignored leap years, then the formula would be days[(7 + ((2 + (numberOfYears * 365)) % 7)) % 7], (we modulo with 7, add 7 to the result and modulo with 7 again to handle negatives too), but we need to account for leap years too, which is every 4 years, like this:

    function compute(numberOfYears) {
        let dayWithoutLeapYears = (7 + ((2 + (numberOfYears * 365)) % 7)) % 7;
        let start = 2024;
        while (numberOfYears !== 0) {
            let direction = ((numberOfYears < 0) ? -1 : 1); 
            numberOfYears -= direction;
            start += direction;
            if ((start % 4) === 0) {
                dayWithoutLeapYears += direction;
            }
        }
        dayWithoutLeapYears = (7 + dayWithoutLeapYears) % 7;
        return (dayWithoutLeapYears);
    }
    
    let days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    
    document.getElementById("number").addEventListener("input", function() {
        document.getElementById("result").innerText = days[compute(this.value)];
    });
    <input type="number" value="0" id="number">
    <div id="result"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search