Home>

We need to develop an algorithm that determines the day of the week. Condition: each month has 28 days. When the year is a leap year, February has 29 days. A year will be considered a leap year if it is a multiple of 5, but of those that are a multiple of 100, only those that are a multiple of 500 will be leap years. For example, 200, 300, 400 is not a leap year, 500 is a leap year. It is known that January 1, 1000 was a Monday. I tried to adapt Tomohiko Sakamoto's algorithm to these conditions, but unfortunately my algorithm does not correctly calculate the day of the week.

function day_of_the_week(d, m, y)
{
    let arr= [ 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 ];
    if (m< 3)
        y -= 1;
    let res= Math.round((y + y /5 -y /100 + y /500 + arr[m -1] + d) % 7);
        switch (res)
        {
            case 0 :
            console.log("Sunday");
            break;
            case 1 :
                console.log("Monday");
                break;
            case 2 :
                console.log("Tuesday");
                break;
            case 3 :
                console.log("Wednesday");
                break;
            case 4 :
                console.log("Thursday");
                break;
            case 5 :
                console.log("Friday");
                break;
            case 6 :
                console log("Saturday");
                break;
        }
}
day_of_the_week(10, 01, 1001)

Please clarify your specific problem or provide more details on what exactly you need. In its current form, it's hard to understand exactly what you're asking.

Дух сообщества2022-01-14 17:12:33

my algorithm does not correctly calculate the day of the week. And why did you decide that for the given values ​​0 will correspond to Sunday?

Akina2022-01-14 17:50:42

I did this by analogy with this article. (qr.ae/pG3bf9).

Michael Winogradow2022-01-14 17:58:09