Hack #43. Calculate Any Weekday

Quickly calculate the day of the week for any date of the Gregorian calendar—useful for scheduling appointments and meetings!

The imperfect Gregorian calendar, when combined with the imperfect Earth year—which is an even multiple of neither 12 (the number of months) nor 7 (the number of days in the week), but instead an icky 365.24237404 days long (approximately!)—means that, for most of us to find what day of the week a meeting falls on, we have to consult a wall calendar or our PDA.

But what if you had your own perpetual calendar in your head? What if you could, with practice, take just a few seconds to calculate any day of the week from centuries ago and into the distant future, when they finally nudge the Earth into a more reasonable orbit?

You can. Here's how.1,2

In Action

To calculate any weekday, you basically need to find four numbers, add them together, and then cast out sevens (i.e., calculate that number modulo 7, a simple procedure). In practice, you can do the modulo math as you go along to keep the numbers small and simply keep a running total.

Here are the numbers you need:

  • The year-item

  • The month-item

  • The day-item

  • Adjustment

The year-item

Finding the year-item (or key number for the year) is easy. Here's the formula:

(YY + (YY div 4)) mod 7

where YY represents the last two digits of the year.

The month-item

Finding the month-item requires some memorization. There are only 12 key numbers, though, one for each month, as shown in Table 4-10.

Table 4-10. Key numbers for calendar months

MonthKey number
January0
February3
March3
April6
May1
June4
July6
August2
September5
October0
November3
December5

To memorize these numbers, you can use any mnemonics that you prefer, such as the Dominic System [Hack #6].

The day-item

The day-item is simply the day of the month—for example, 1 for April 1, 31 for October 31, 15 for March 15, and so on.

Adjustment

The fourth number you will need to find is an adjustment to the total. It has two parts: the century-item, plus a possible tweak if the year is a leap year.

Since you'll mostly be finding dates in the 20th and 21st centuries, you can probably ignore most of Table 4-11, and just remember that for dates from 1900–1999, the adjustment is 0 (that is, don't add anything), and for dates from 2000–2099, you add 6.

Table 4-11. Century-item adjustments

CenturyAdjustment
1700s4
1800s2
1900s0
2000s6
2100s4
2200s2
2300s0

A more precise method follows.

The Julian calendar ended in most Western countries on September 2, 1752, and the Gregorian calendar began on September 14, 1752.

Tip

They had to fudge the date when they converted over. Legend has it that people rioted for their lost days. And you thought Y2K was a big deal.

To get the century-item for any date on the Julian calendar, subtract the century (which would be 14 for the year 1492) from 18, and cast out sevens.

To get the century-item for any Gregorian year up to 9999, take the first two digits of the year, cast out fours, subtract the result from 3, and multiply the difference by 2. Thus, for the year 2008:

20 mod 4  = 0
3 - 0     = 3
3 × 2     = 6

And 6 is indeed the century-item for the 2000s, as shown in Table 4-11.

Now for the leap-year tweak. If your date is in January or February of a leap year, subtract 1 from the running total.

Any year evenly divisible by 4 in the Gregorian calendar is a leap year, except that years also divisible by 100 aren't—except that years also divisible by 400 are leap years. Thus, 1936 was a leap year (it is evenly divisible by 4); 1937 was not a leap year (it's not divisible by 4). The years 1800 and 1900 weren't leap years (they're evenly divisible by 100), but 2000 was (it's also evenly divisible by 400).

Leap years in the Julian calendar are simply any year divisible by 4.

What to do with the result

If you have cast out all sevens (that is, calculated the number mod 7 correctly), the end result will be a number from 0 to 6. This number will tell you the weekday, starting with Sunday at 0 and counting upward, as shown in Table 4-12.

Table 4-12. Weekdays and corresponding results

WeekdayResult
Sunday0
Monday1
Tuesday2
Wednesday3
Thursday4
Friday5
Saturday6

In Real Life

Let's calculate the weekday of the first moonwalk, July 21, 1969:

  1. The key number for the year is (69 + (69 div 4)) mod 7.

  2. 69div4=17, because 69/4=17 with a remainder of 1 (or 17r1).

  3. 69+17=86.

  4. Now, cast out sevens: 86 mod 7 = 2, because the next highest multiple of 7 is 84, and 86 – 84 = 2. Remember the number 2.

  5. The key number for the month of July is 6 (see Table 4-10). Add that key number to the result you remembered in the previous step: 6 + 2 = 8.

  6. Cast out sevens: 8 mod 7 = 1. (7 goes into 8 once, with 1 left over.) Remember the number 1.

  7. The key number for the day is 21, because the first moonwalk took place on July 21. Add the result you remembered in the previous step: 21 + 1 = 22.

  8. Cast out sevens: 22 mod 7 = 1.

  9. The adjustment is 0, because this took place from 1900–1999 and it was not January or February of a leap year: 1 + 0 = 1.

  10. The final result is 1, so the first moonwalk took place on a Monday. (And consulting a calendar, we find that this is true!)

To be precise, the moonwalk took place at 2:39:33 A.M. UTC, which was Sunday night throughout the U.S. I bet the workers of the world had quite a bit to talk about around the water cooler the next day.

End Notes

  1. Carroll, Lewis. "Lewis Carroll's Algorithm for finding the day of the week for any given date." http://www.cs.usyd.edu.au/~kev/pp/TUTORIALS/1b/carroll.html.

  2. Mentat Wiki. "Calendar Feat." http://www.ludism.org/mentat/Calendar Feat (explains several different ways to calculate the date, and many shortcuts).

See Also

Get Mind Performance Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.