TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to Perform Calendar Calculations in Your Head

17 pointsby jorgenveisdalover 5 years ago

2 comments

amflareover 5 years ago
Given that you&#x27;ll rarely need more than this year, and maybe next year, you can preemptively do some math to make it easier.<p><pre><code> yearcode = (centurycode + [last two digits of year] + ([last two digits of year] div 4)) mod 7 </code></pre> 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.<p>This link [0] explains the process from this perspective<p><pre><code> [0]: https:&#x2F;&#x2F;litemind.com&#x2F;how-to-become-a-human-calendar&#x2F;</code></pre>
tzsover 5 years ago
This can be made much more friendly to in your head calculation. In the following, I shall use Python 3 operators. In particular X&#x2F;&#x2F;Y means the integer part of X&#x2F;Y. I will use &#x27;x&#x27; for multiplication instead of the usual splat, so I don&#x27;t have to deal with HN trying to interpret it as a formatting code.<p>The article computes the year code like this, where Y is the last two digits of the year:<p><pre><code> Y + Y&#x2F;&#x2F;4 </code></pre> For the example in the article, 1989, that gives 89 + 89&#x2F;&#x2F;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.<p>It&#x27;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<p><pre><code> Y + Y&#x2F;&#x2F;4 </code></pre> but that have smaller intermediate values. One of these is called the &quot;odd + 11&quot; method, and goes like this:<p><pre><code> if odd(Y): Y += 11 Y = Y&#x2F;&#x2F;2 if odd(Y): Y += 11 Y = -Y return Y </code></pre> For Y=89, we&#x27;d see that it is odd, add 11 giving 100, divide by 2 giving 50. Any time after the Y&#x2F;&#x2F;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.<p>Another method, that keeps the intermediate numbers even smaller, is this. This method operates on the individual digits of the two digit year. Let&#x27;s call these T and U, with Y = 10 T + U.<p><pre><code> C = 2 x T if odd(T): C += 3 C += U if odd(T): if U &gt;= 2: C += 1 if U &gt;= 6: C += 1 else: if U &gt;= 4: C += 1 if U &gt;= 8: C += 1 return C </code></pre> 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 &#x27;if&#x27; morass adds 2 more, bringing us to 6.<p>The mass of &#x27;if&#x27; 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&#x27;s how you should probably actually remember it and use it. The if&#x27;s are just there for writing it in pseudo-code.<p>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.<p>The month corrections and century corrections given in the article are old. I first saw them in Martin Gardner&#x27;s &quot;Mathematical Games&quot; column in &quot;Scientific American&quot; around 50ish years ago. These are NOT the corrections most people use now. Instead of ADDING the numbers given:<p><pre><code> 1 4 4 0 2 5 0 3 6 1 4 6 </code></pre> with these century corrections (for 16xx, 17xx, 18xx, 19xx):<p><pre><code> 6 4 2 0 </code></pre> what most people do now is SUBTRACT these:<p><pre><code> 3 0 0 4 2 6 4 1 5 3 0 2 </code></pre> with these century corrections:<p><pre><code> 2 0 5 3 </code></pre> (Oh, and most people use Sunday = 0 nowadays, not Saturday = 0 as given in the article. That&#x27;s also taken into account by the month&#x2F;century corrections).<p>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).<p>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&#x27;s correction is September&#x27;s month number (9, or 2 after reducing mod 7), and September&#x27;s is May&#x27;s month number (5). Similar for July and November, each using the other&#x27;s month number as its correction.<p>That only leaves January and February, which you just have to memorize as 3 and 0. For leap years, those become 4 and 1.<p>If C is the first two digits of the 4 digit year (e.g., 19 for year 1989), the century correction is:<p><pre><code> 5 x (C % 4) + 2 </code></pre> For 19, that&#x27;s 5 x 3 + 2 = 3 after a mod 7 reduction.<p>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&#x2F;month corrections I&#x27;m using are set for Sunday = 0).<p>With a little practice, you can get pretty fast with the tens&#x2F;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&#x27;ll probably quickly end up automatically memorizing the century corrections.