Compute the day of the week

Here's a simple algorithm to compute the day of the week of any given date.

  1. Take the last two digits of the year.
  2. Add the number of times four divides that number.
  3. If the date is in the 20th century, add one. If the date is in the 22nd century, subtract one.
  4. 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.
  5. If the date is in january or february of a leap year, subtract one.
  6. Add the day of the month.
  7. Divide by seven. The remainder is the answer, with 1 being monday.

Example