
How to Perform Calendar Calculations in Your Head - jorgenveisdal
https://medium.com/cantors-paradise/how-to-perform-calendar-calculations-5617f35d3070
======
amflare
Given that you'll rarely need more than this year, and maybe next year, you
can preemptively do some math to make it easier.

    
    
        yearcode = (centurycode + [last two digits of year] + ([last two digits of year] div 4)) mod 7
    

So this year (2019) = 1, and next year (2020) = 3 (and also a leap year).
These numbers are infinitely easier to keep track of, especially when doing
modulo operations in your head.

This link [0] explains the process from this perspective

    
    
      [0]: https://litemind.com/how-to-become-a-human-calendar/

------
tzs
This can be made much more friendly to in your head calculation. In the
following, I shall use Python 3 operators. In particular X//Y means the
integer part of X/Y. I will use 'x' for multiplication instead of the usual
splat, so I don't have to deal with HN trying to interpret it as a formatting
code.

The article computes the year code like this, where Y is the last two digits
of the year:

    
    
      Y + Y//4
    

For the example in the article, 1989, that gives 89 + 89//4 = 111. It then
adds in numbers to deal with the month and the day of the month, and finally
at the end reduces the whole thing mod 7 to get a day of the week.

It's a lot easier mentally if you can keep the intermediate numbers smaller.
One way to do that is to reduce mod 7 along the way. Another way is to use
procedures that are a little more complicated than

    
    
      Y + Y//4
    

but that have smaller intermediate values. One of these is called the "odd +
11" method, and goes like this:

    
    
      if odd(Y):
        Y += 11
      Y = Y//2
      if odd(Y):
        Y += 11
      Y = -Y
      return Y
    

For Y=89, we'd see that it is odd, add 11 giving 100, divide by 2 giving 50.
Any time after the Y//2, it is OK to toss in a mod 7 reduction. Doing that to
50 gives 1. Negate that, and do another mod 7, and we get 6.

Another method, that keeps the intermediate numbers even smaller, is this.
This method operates on the individual digits of the two digit year. Let's
call these T and U, with Y = 10 T + U.

    
    
      C = 2 x T
      if odd(T):
        C += 3
      C += U
      if odd(T):
        if U >= 2:
          C += 1
        if U >= 6:
         C += 1
      else:
        if U >= 4:
          C += 1
        if U >= 8:
         C += 1
      return C
    

You can do mod 7 reductions anywhere through that. For 89, we have T=8, U=9. 2
x T = 2 mod 7. T is not odd, so skip adding 3, leaving us still at 2. Adding
in U mod 7 brings us to 4. Finally that scary looking 'if' morass adds 2 more,
bringing us to 6.

The mass of 'if' statements are simpler than they look. They are dealing with
leap years, and the rule is simply that if T is even, there are leap years at
U=4 and U=8, each adding 1 to the running total. If T is odd, the leap years
come at U=2 and U=6. That's how you should probably actually remember it and
use it. The if's are just there for writing it in pseudo-code.

The month numbers and the century corrections balance each other, in the sense
that you can add or subtract a constant to all the month numbers, and subtract
or add the same constant to the century correction, without changing the final
day.

The month corrections and century corrections given in the article are old. I
first saw them in Martin Gardner's "Mathematical Games" column in "Scientific
American" around 50ish years ago. These are NOT the corrections most people
use now. Instead of ADDING the numbers given:

    
    
      1 4 4 0 2 5 0 3 6 1 4 6
    

with these century corrections (for 16xx, 17xx, 18xx, 19xx):

    
    
      6 4 2 0
    

what most people do now is SUBTRACT these:

    
    
      3 0 0 4 2 6 4 1 5 3 0 2
    

with these century corrections:

    
    
      2 0 5 3
    

(Oh, and most people use Sunday = 0 nowadays, not Saturday = 0 as given in the
article. That's also taken into account by the month/century corrections).

A way to remember those month correction for the even months starting at April
is that the month correction equals the month number, so that gets us April
(4), June (6), August (8 mod 7 = 1), October (10 mod 7 = 3), and December (12
mod 7 = 2).

For May, July, September, and November, the correction for each is the month
number of the one two away from it in that list. So May's correction is
September's month number (9, or 2 after reducing mod 7), and September's is
May's month number (5). Similar for July and November, each using the other's
month number as its correction.

That only leaves January and February, which you just have to memorize as 3
and 0. For leap years, those become 4 and 1.

If C is the first two digits of the 4 digit year (e.g., 19 for year 1989), the
century correction is:

    
    
      5 x (C % 4) + 2
    

For 19, that's 5 x 3 + 2 = 3 after a mod 7 reduction.

For the date given in the article, Jan 13, 1989, that gives us 3 (century) + 6
(year) - 3 (month) + 13 (day) = 5 mod 7, which is Friday, same as the article
got (remember, the century/month corrections I'm using are set for Sunday =
0).

With a little practice, you can get pretty fast with the tens/units year
correction calculation, and with recalling the month correction via the
mnemonic aids given above. Almost all dates you will have to deal with will be
in just two centuries, so you'll probably quickly end up automatically
memorizing the century corrections.

