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.

Mental math - Find the day of the week a given date falls on

2 pointsby exuperoover 11 years ago

1 comment

ColinWrightover 11 years ago
There are many ways to do this, and the one given is one I really dislike. A lot.<p>Here&#x27;s the one I use.<p><pre><code> Each year has a magic number. For 2013 it&#x27;s 1. You can compute the magic number for the year. Subtract 1900 (or 2012). Divide by four, round down, and add it on. The magic number is (Y + Y&#x2F;4) % 7 Each year has a magic number: 1 4 4 (so January is 1) 0 2 5 0 3 6 (so August is 3) 1 4 6 (so November is 4) You can compute these, but I don&#x27;t recommend it. Add these two magic numbers, the day of the month, and compute the remainder mod 7. Then Sunday = 1 Wednesday = 4 etc. </code></pre> So Dec 25th, 2013 is given by (1 + 6 + 25) % 7 = Wednesday.<p>An adjust is required for dates in January or February in leap years.<p>Conway&#x27;s &quot;Doomsday Algorithm&quot;[0][1] is an alternative, and while Art Benjamin uses the same technique I do, he teaches a variant that he claims is easier to remember.<p>[0] <a href="http://en.wikipedia.org/wiki/Doomsday_rule" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Doomsday_rule</a><p>[1] <a href="http://www.timeanddate.com/date/doomsday-weekday.html" rel="nofollow">http:&#x2F;&#x2F;www.timeanddate.com&#x2F;date&#x2F;doomsday-weekday.html</a>