Compute the day of the week
Here's a simple algorithm to compute the day of the week of any given date.
- Take the last two digits of the year.
- Add the number of times four divides that number.
- If the date is in the 20th century, add one. If the date is in the 22nd century, subtract one.
- Add a magic constant for the month. Starting from January, the constants are: 6, 2, 2, 5, 0, 3, 5, 1, 4, 6,
2 and 4.
- If the date is in january or february of a leap year, subtract one.
- Add the day of the month.
- Divide by seven. The remainder is the answer, with 1 being monday.
Example