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.

Calculate the day of the week for any date in your head

233 pointsby emrehanover 5 years ago

14 comments

bumbledravenover 5 years ago
Here&#x27;s the easiest way I’ve seen [1] to determine if a year is a leap year:<p><i>If the last two digits of the year are 00, throw them away and keep only the first two digits. Otherwise, keep only the last two digits. The year is a leap year if what you kept is divisible by 4.</i><p>Examples:<p>- The year 1705 does not end in 00, so throw away 17 and keep 05. 05 <i>is not</i> divisible by 4, so 1705 <i>is not</i> a leap year.<p>- The year 2000 ends in 00, so throw away 00 and keep 20. 20 <i>is</i> divisible by 4, so 2000 <i>is</i> a leap year.<p>The standard method [2] is more complicated:<p>&gt; Years that are divisible by four are leap years, with the exception of years that are divisible by 100, which are not leap years, and with the final exception of years divisible by 400, which are.<p>[1] <a href="https:&#x2F;&#x2F;twitter.com&#x2F;jordancurve&#x2F;status&#x2F;1012203999716618240" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;jordancurve&#x2F;status&#x2F;1012203999716618240</a><p>[2] <a href="https:&#x2F;&#x2F;www.theguardian.com&#x2F;science&#x2F;2011&#x2F;feb&#x2F;28&#x2F;leap-year-alex-bellos" rel="nofollow">https:&#x2F;&#x2F;www.theguardian.com&#x2F;science&#x2F;2011&#x2F;feb&#x2F;28&#x2F;leap-year-al...</a>
评论 #21928886 未加载
IgorPartolaover 5 years ago
If not for religious groups, we could have had World Calendar and all this would be unnecessary: <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;World_Calendar" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;World_Calendar</a>.
评论 #21928384 未加载
评论 #21928408 未加载
评论 #21928679 未加载
评论 #21930501 未加载
评论 #21928540 未加载
评论 #21928273 未加载
dingalingover 5 years ago
This looks like an algorithm that could be useful if programmed rather than carried around in one&#x27;s head.<p>It also still requires a lookup to bootstrap it: the day-of-week of the last day of February for the selected year. So it&#x27;s a clever and well-observed compilation of identified patterns, but not self-contained.
评论 #21927082 未加载
评论 #21927049 未加载
评论 #21927527 未加载
hunta2097over 5 years ago
My Autistic son does this in a couple of seconds. He can only really do the last 50 years tho.<p>Savants who do this are called &quot;human calendars&quot;.
评论 #21928012 未加载
bumbledravenover 5 years ago
A simpler and easier way to calculate the day of the week for any date is the <i>First Sunday Doomsday algorithm</i>: <a href="http:&#x2F;&#x2F;firstsundaydoomsday.blogspot.com&#x2F;2009&#x2F;12&#x2F;quick-start-guide.html" rel="nofollow">http:&#x2F;&#x2F;firstsundaydoomsday.blogspot.com&#x2F;2009&#x2F;12&#x2F;quick-start-...</a><p>(There’s also a earn-by-example page: <a href="http:&#x2F;&#x2F;firstsundaydoomsday.blogspot.com&#x2F;2011&#x2F;01&#x2F;learn-by-example.html" rel="nofollow">http:&#x2F;&#x2F;firstsundaydoomsday.blogspot.com&#x2F;2011&#x2F;01&#x2F;learn-by-exa...</a>)<p>Using Sundays rather than doomsdays simplifies the arithmetic. Another improvement the above approach incorporates is the odd+11 rule for calculating the year code.
sverhagenover 5 years ago
For all but the most basic details my rule is that if I have to remember it, the system is broken. I know a small handful of passwords off the top of my head, a few email addresses, my wife&#x27;s and my phone numbers, and the phone number of my home during childhood. (I&#x27;m obviously exaggerating, you know, for dramatic effect.) This trick requires so many mnemonic devices that it makes my head spin. It&#x27;s still impressive if you can do it. Just not for me.
评论 #21927263 未加载
评论 #21929008 未加载
评论 #21927324 未加载
sethammonsover 5 years ago
&quot;[hey seri|ok google|alexa], what day does April 8th fall on?&quot;
jb3689over 5 years ago
The 4-6-8-... and 9-5-7-11 rules are really nice to have on hand. The rest of relating years; 99% of the time I&#x27;m going to be calculating dates for the current year so those seem less useful to me (not to mention that part is a lot more complicated to remember)
toolsliveover 5 years ago
This is how I do it: <a href="https:&#x2F;&#x2F;incubaid.wordpress.com&#x2F;category&#x2F;algorithms&#x2F;" rel="nofollow">https:&#x2F;&#x2F;incubaid.wordpress.com&#x2F;category&#x2F;algorithms&#x2F;</a>
cowsandmilkover 5 years ago
&gt; There are actually 52 (or 53) other days which are all on the same day of the week as &quot;the&quot; Doomsday at the end of February<p>That seems incorrect, there should always be 51 other days
评论 #21927971 未加载
papafoxover 5 years ago
Doesn&#x27;t Zellers&#x27; Congruence do the same task? Zellers&#x27; has been around since 1883, and has been very widely implemented.
tzsover 5 years ago
Here&#x27;s a summary of the methods I know for computing the factor based on the last two digits of the year that gets added to the factor from the century number to get the Doomsday for the year.<p>Notation: &quot;A &#x2F;&#x2F; B&quot; is integer vision (think Python 3 &#x2F;&#x2F; operator). &quot;A, B = C &#x2F;% D&quot; means that &quot;A = C&#x2F;&#x2F;D&quot; and &quot;B = C % D&quot;. &quot;A ≡ B&quot; means A and B are the same mod 7. &quot;A ≡⁴ B&quot; means A and B are the same mod 4. I&#x27;ll use x for multiplication. X + &lt;a,b,c,d&gt; means the four values A+a, X+b, X+c, X+d.<p>Let Y be the last two digits of the year.<p>Observation: 365 ≡ 1. If it weren&#x27;t for leap years, the year factor would simply go up by 1 each year. A leap year pushes the start of the next year back a day, and that&#x27;s cumulative, so we need a correction of the number of leap years that have past.<p>1. That gives the first, and simplest, way to compute the year factor for year Y.<p><pre><code> return Y + Y&#x2F;&#x2F;4 </code></pre> Advantage: simplest algorithm.<p>Disadvantage: numbers get larger than you might be comfortable with for fast mental meth. Doing 99, for example, gives 99 + 99&#x2F;4 = 99 + 24 = 123 ≡ 4, which has plenty of opportunity along the way to goof.<p>Many people will do better with a method that uses a little more complicated algorithm but cuts down the size of the numbers.<p>2. The divide by 12 method, which is the one given in the article, does this.<p><pre><code> a, b = Y &#x2F;% 12 c = b &#x2F;&#x2F; 4 return a + b + c </code></pre> Here&#x27;s why it works.<p><pre><code> Y = 12 a + b Y&#x2F;&#x2F;4 = 3 a + b&#x2F;&#x2F;4 = 3 a + c Y + Y&#x2F;&#x2F;4 = 15 a + b + c ≡ a + b + c </code></pre> Advantage: a &lt;= 8, b &lt; 12, c &lt; 4. Other than the first &#x2F;%, you only deal with fairly small numbers.<p>Disadvantage: Still easy for many people to goof on the initial &#x2F;%.<p>Note: if you divide by 20 instead of 12, you get a similar method, except that you&#x27;ll need to use 4a instead of a. Since a is at most 4, 4a is not large. For many people %&#x2F; 20 might be sufficiently easier to deal with in mental math than %&#x2F; 12 that it is worth the cost of having to multiply by 4.<p>3. The odd 11 method.<p><pre><code> Y += 11 if Y is odd Y &#x2F;&#x2F;= 2 Y += 11 if Y is odd return -Y </code></pre> The easiest way to see that this works is to go back to Y + Y&#x2F;&#x2F;4. Write Y = 4q + r, where r = 0, 1, 2, or 3.<p>The first conditional add 11 step turns 4q + &lt;0,1,2,3&gt; into 4q + &lt;0,12,2,14&gt;. Dividing by 2 then gives 2q + &lt;0,6,1,7&gt;. The second conditional add 11 gives 2q + &lt;0,6,12,18&gt;. Negating mod 7 gives 5q + &lt;0,1,2,3&gt;, which is 4q + &lt;0,1,2,3&gt; + q = Y + Y&#x2F;&#x2F;4.<p>Similar considerations give other divide by 2 and negate methods. If you note that if Y = 4q, then Y + Y&#x2F;&#x2F;4 ≡ -(Y&#x2F;&#x2F;2), then it is just a matter of how to take into account the r in 4q + r when r != 0. One way to do that would be first subtract r, then do the -(Y&#x2F;&#x2F;2), and then add r back. That works because none of the r != 0 years are leap years, and so we just need to add 1 for each of them.<p>Another way would be to just go ahead and do -(Y&#x2F;&#x2F;2), and then add in a correction based on r. Let Y = 4q + &lt;0,1,2,3&gt;. Then -(Y&#x2F;&#x2F;2) ≡ 5q + &lt;0,0,-1,-1&gt;. We want 5q + &lt;0,1,2,3&gt;, so we have to add 0, 1, 3, or 4 if r = 0, 1, 2, 3, respectively.<p>That last correction can be split into two parts: add 1 if Y is odd, plus add 2 if Y&#x2F;&#x2F;2 is odd. That gives:<p><pre><code> t = Y odd ? 1 : 0 Y &#x2F;&#x2F;= 2 t += 3 if Y is odd return -Y + t </code></pre> 4. The method I actually use, which views the two digit year as a decade part and a year within the decade part, and operates on them separately. Since I gave a long description of it yesterday, I&#x27;ll just link to that [1] here.<p>[1] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21926543" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21926543</a>
bubba1236over 5 years ago
this is a great article went through it and can do it now
kissgyorgyover 5 years ago
It&#x27;s a nice article and a good algorithm, kudos to all who can do this.<p>My philosophy however is that I don&#x27;t need to learn this kind of stuff nowadays. There are computers even in our wrist, so I offload as much as I can, and spare my brain cycles more important stuff that matters.