5 ms·
A faster way to calculate the day of the week
- rho4 2mo agoSo are there no discontinuities for weekday calculation? Never 2 Sundays in a row or a maybe even a Monday skipped?
- ForHackernews 2mo agoNot within a single calendar system, I think. There have been discontinuities when transitioning between them. Sweden skipped 11 days in 1753: https://todayinhistory.app/events/12868/ https://todayinhistory.app/events/12868/
- balgg 2mo agoMy comment isn't really about the content itself but... this is what I want the web to be. Documents that take advantage being on the web (visualizations, interactivity) while still remaining structured and simple. I really like the overall visual style too, incredible work. /glaze
- benjoffe 2mo agoThanks! I'm pretty sure with this one I spent more time working on the blog post than I did developing the algorithms. I would not normally spend such effort, but in this case I felt that the functions were too hard to understand without the visual aid, and too difficult to customise without the Function Explorer. Then I think I just got a bit carried away with the detail.
- eru 2mo agoThanks for putting in the effort. You might also https://www.redblobgames.com/ https://www.redblobgames.com/ and learn a lot from how they do their presentations.
- honr 2mo agoThe theory is trivial for anyone with a Number Theory background. That's not the point. Picking a good and interesting problem and going all the way to an elegant optimal solution, AND presenting it well, is all that counts. Without the good presentation and proof, it could look like a hack / weird trick to a random reviewer. I have personally seen a few good algorithms of this sort being dismissed in codebases because they were hard for people to understand (or more precisely, the author hadn't been skilled / diligent enough to leave a good demonstration linked in the code). I wish there was a place for good algorithms like this, where, unlike plain wikipedia, the good visualized proof could stay and serve. Or perhaps there are such places and I don't know about them. Bret Victor did a bunch of work around visualizations ~12 years ago (IIRC) for teaching and showing math, but I am not sure if there was any following on that path.
- ForHackernews 2mo agoYou should consider publishing some of your work at https://pudding.cool/ https://pudding.cool/ - I think it would fit in well there.
- jph 2mo agoSeconded. Ben, you have a great teaching style, thank you for sharing this.
- BrtByte 2mo agoI miss when "using the web" meant enhancing a document rather than replacing it with 4MB of JS and a dashboard
- jocelyner 2mo ago[dead]
- rmunn 2mo agoExcellent article, and very useful for programming. For figuring out the day of the week in your head, there's a different (and very useful) trick. It uses the month and day from the Gregorian calendar, rather than the day number, because 99% of the time that's what you'll be wanting: "Which day of the week will August 31st, 2026 be?" The trick is to notice that in any year, leap year or not, the following dates will be the same day of the week in any year: 4/4, 6/6, 8/8, 10/10, and 12/12. (Whether you use the month/day style favored in America, or the day/month style that the rest of the world uses, either way those dates are the same, which is convenient). Which day of the week those dates are will vary from year to year: this year (2026) those are all Saturdays, last year they were all Fridays, next year they will all be Sundays. But in any given year, those dates are separated by 63 days, a multiple of seven, so they are always going to be the same day of the week. And that's not all: look at 5/9, 9/5, 7/11, and 11/7 — those are also that same day of the week! (Again, conveniently, this works whether you're using the American calendar style or the rest of the world). Those odd-numbered dates can easily be remembered with the mnemonic phrase "a 9-to-5 job at 7-11". And voila, you have memorized one "special" day in nine of the twelve months. (What you do for January, February, and March I will cover later). Now I'll pause and explain why this is useful. Many of you have already made the intuitive leap to figure this out, but it's still useful to see it spelled out. If you're trying to figure out what day of the week a certain date, let's say August 31st, is going to be, then you find the "key" date for that month (August 8th), and remember that this year, those key dates are Saturdays. (I don't yet have a good mnemonic for easily remembering which key date is which for each year; I'd welcome suggestions if you can think of one). So, 8/8 was a Saturday. Therefore, August 15th and 22nd and 29th are Saturdays. Therefore, August 31st will be a Monday this year. All of that is math you can easily do in your head in seconds. Now, January, February, and March. March is always the same in every year: March 7th. 14th, 21st, and 28th are the "key days" that are the same day as 4/4 et al. But the way I prefer to memorize March is that "March 0th" (which isn't actually in March, it's the last day of February) is the "key day". That still lets me do the mental math for March easily enough, but it also reminds me that Feburary 28th or 29th, depending on whether you're in a leap year, is the "key day" for this year. Finally, for January, I use January 3rd in non-leap-years and January 4th in leap years as the "key day" that I memorize; the number 4 is strongly associated with leap years, so "leap year = January 4th" is easy to memorize, and I just have to remember that it's one day less in non-leap-years, January 3rd. And so I just have to memorize "January 3rd or 4th" and "March 0th", plus the phrase "a 9-to-5 job at 7-11". And I can easily figure out in my head what day of the week any day of the year will be. For example, December 25th will be a Friday this year: that took me three seconds to work out in my head without looking at a calendar.
- xiaodai 2mo agolook up table?
- userbinator 2mo agoI can't readily think of an application in which this is a performance-critical computation, so I'd be tempted to optimise for size, but in the more general sense of "how to quickly obtain the modulus of dividing by 7", this becomes a much more practical use.
- tobin1994 2mo ago[dead]
- alt227 2mo agoBeautiful page. Great style, enjoyable writing, and excellent clear diagrams and equations. Well done!
- gblargg 2mo agoNot covered in the page, but dates can be a negative number of days from the epoch, which is what the fixups (+7) and using mod 7 twice are for, as negative mod 7 will yield a negative.
- masklinn 2mo agoIt is in fact covered in the page, as the heading for that formula specifically calls out “languages with signed "%", eg. C/C++”. That is why there is a formula without that adjustment with a “posmod” aka any modulo where a positive divisor yields a positive value (Euclidean division as in rust’s `rem_euclid`, but also floored division as in Python or Ruby, langages with both mod and rem functions / operators generally have a floored division on mod and a truncated division on rem but that’s not a guarantee so check).
- gblargg 2mo agoJust saying, a little clarification would have helped. The details are there, but putting it together would have laid out the base algorithm that's being optimized, which I see as a basic point of an article like this, give everyone a clear understanding of the algorithm so they can follow along. "rd is number of days offset from 1970-01-01, positive or negative. In some languages the mod 7 can yield a negative in the range -6 to 0, so an extra +7 and mod is used in the basic implementation."
- WorseIsBetter 2mo agoDoomsday rule for the brain, mul-add-shift for the machine. This article nails both. And those circle visualizations? Chef's kiss
- rackp 2mo agoSpeaking of calculating the day of the week, I remember seeing someone on TV about twenty years ago who could correctly tell you the day of the week for any given date. I doubt they were doing such complex calculations, so they might have had a special method. If I recall correctly, it was a young boy with savant syndrome, so he might have simply memorized an incredible number of patterns...
- unfunco 2mo agoThere was this[0] famous answer on University Challenge, but it's a pretty simple calculation. [0]: https://www.youtube.com/watch?v=-dh7COSiB7g https://www.youtube.com/watch?v=-dh7COSiB7g
- marysol5 2mo ago"We need Jungle I'm afraid" https://www.youtube.com/shorts/8jZUTPPf6do https://www.youtube.com/shorts/8jZUTPPf6do
- mnw21cam 2mo agoSee the Doomsday algorithm, discussed elsewhere on this thread. https://en.wikipedia.org/wiki/Doomsday_rule https://en.wikipedia.org/wiki/Doomsday_rule
- walrus01 2mo agoOn a slightly related topic I was recently thinking of how simple it is to only have to worry about one calendar in your personal and business life (the western gregorian calendar). If you're an even slightly observant Muslim with an educated techincal/professional life in many parts of the historical Persian speaking world, you have three calendars to keep track of: 1) Western gregorian calendar (doing business with foreigners, international travel, banking, finance, most everything done on the Internet, etc). 2) Islamic arabic originated lunar calendar (When does Ramadan start, when is an okay time to go for hajj, when is Eid Al-Fitr, when is Eid Al-Adha, etc). This goes "backwards" 10 to 11 days per year compared to the western gregorian calendar so in different decades Ramadan might be in the middle of summer, or middle of winter. 3) Persian solar calendar for new year observances like Nowruz: https://en.wikipedia.org/wiki/Nowruz https://en.wikipedia.org/wiki/Nowruz , and generally persian-related cultural things: https://en.wikipedia.org/wiki/Solar_Hijri_calendar https://en.wikipedia.org/wiki/Solar_Hijri_calendar But wait, there's more! Let's say you live in Uzbekistan or Tajikistan or that region and you do business with Russians. You might have to think about when the Russians are on vacation and businesses are closed, because they're taking holidays and religious observances based on the eastern orthodox calendar. Basically all the same problem as trying to get business things done in the UK on a UK standard "Bank Holdiay". Their Christmas is not the same as the western Gregorian calendar Christmas... https://en.wikipedia.org/wiki/Eastern_Orthodox_liturgical_calendar https://en.wikipedia.org/wiki/Eastern_Orthodox_liturgical_ca...
- tacostakohashi 2mo agoIt depends what kind of business... there are all sorts of weird and wonderful accounting / financial calendar inventions like the 4-4-5 calendar, fiscal years ending in June, bond day count conventions like 30/360, etc. I do take your point though. I've always found it quite surprising just how universal various arbitrary/western things are like, as you say, the western gregorian calendar, seconds/minutes/hours, SI units for measurement. In a world with so many unintelligible languages and disagreements, you'd think there would be more folks out there doing, I dunno, a 10 day week, or some non-hour/minute system, etc.
- 2mo ago
- srean 2mo agoIf only the periods of the annual motion of the sun, the periodicity of the phases of the Moon and the unit of day were not mutually prime. If only they were in resonance.
- dfdsvfdvd 2mo agoif day = "friday" then output = :) else output = :(
- nk_kolja 2mo agoNice! And what a beautiful website! Below the surface it's all the division with constant trick.
- ForHackernews 2mo agoEarlier, from the same author https://news.ycombinator.com/item?id=46020193 https://news.ycombinator.com/item?id=46020193
- BrtByte 2mo agoThis is exactly the kind of optimization rabbit hole I enjoy
- whatever1 2mo agoI am done with time, extra seconds, missing days, time zones, daylight savings, relativity. Distance from the big bang from now on.