What is the easiest algorithm to find the day of week of day zero of a given year?
Here's a simple one-liner. I've verified this for all the years 1901-2200 using Excel, and 1582-3000 using Python's datetime
.
dayOfWeek = (year*365 + trunc((year-1) / 4) - trunc((year-1) / 100) +
trunc((year-1) / 400)) % 7
This will give the day of the week as 0 = Sunday, 6 = Saturday. This result can easily be adjusted by adding a constant before or after the modulo 7. For example to match Python's convention of 0 = Monday, add 6 before the modulo.
int dayofweek(y, m, d) /* 0 = Sunday */
int y, m, d; /* 1 <= m <= 12, y > 1752 or so */
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
Most languages provide facilities for representing and manipulating dates... I would rely on those instead of implementing some (probably incomplete) algorithm.