Calculating Days of the Week (In Your Head)

On what date does Easter fall this year?  What day of the week is your dad’s birthday?  What day of the week is July 4th this year?  Answering questions like this, quickly, without consulting a calendar, seems to be a frequent parlor trick recently.  This is a quick post describing the approach that I use to calculate the day of the week of a given date (i.e., year, month, and day).

Note that there are really two common applications here.  One is an algorithm for a computer.  In that case, I think the best approach is to generalize slightly, using the (modified) Julian day number or something similar to associate consecutive dates with consecutive integers.  That algorithm is well-documented.  The other application is a method for performing the calculation quickly in your head, which is a different problem, and is what I am focusing on here.

There is plenty of literature on this problem as well, the most commonly referenced methods being Conway’s Doomsday algorithm and Zeller’s congruence.  The Doomsday algorithm never really appealed to me, seeming to consist of a hierarchical set of special cases progressing from months to years to centuries.  And Zeller’s congruence is often presented with the accurate but complicated arithmetic involving the month, which is fine for a computer, but I certainly can’t do it in my head.

The following approach is one that I remember, with some fondness, working out in the backseat of my parents’ station wagon while traveling to visit my grandparents.  I had found a book (from the public library, now lost to the mists of antiquity) that contained a perpetual calendar, a two-page table that involved a complex sequence of row/column intersections to determine the day of the week for a given date.  I was fascinated by this table, and in an effort to understand how it worked, I tried to turn that complex sequence of steps tracing through the table into a mathematical formula.  In hindsight, the result was effectively Zeller’s congruence, but with some slight modifications that I find to be simpler for computing in your head… although this is perhaps only because it is how I learned it.

The algorithm: Represent the year as 100c + y, 0 \leq y < 100, considering dates in January and February to be in the previous year.  Then given also the month m \in \left\{1, 2, ..., 12\right\} and day d \in \left\{1, 2, ..., 31\right\}, the day of the week w is given by:

w \equiv 5(c\bmod 4) + y + \lfloor\frac{y}{4}\rfloor + M_m + d \pmod{7}

M = (0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4)

where w=0 corresponds to Sunday, w=1 to Monday, etc.  For example, New Year’s Day 2012 will correspond to c=20, y=11, m=1, d=1, yielding w \equiv 0, a Sunday.  Note that this formula works for any non-negative century in the (proleptic) Gregorian calendar.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s