Speaking 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 17 minutes ago [-]
There was this[0] famous answer on University Challenge, but it's a pretty simple calculation.
Excellent 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.
teiferer 3 hours ago [-]
Wow, this is fantastic! That's what I expected from the original article! Instead it "merely" presented a quick signed-mod-7 trick for 32- and 64-bit architectures. Interesting, but given day/month/year is what I actually face and your trick here is exactly what I was looking for! Thanks so much, I'll use this the rest of my life!
rmunn 4 hours ago [-]
For keeping track of the "key day of the week" from year to year: as you move forward through the years, it advances by one when you move into a non-leap-year, and advances by 2 when you move into a leap year. This year (2026) those days are all Saturdays. Next year, 2027, they will all be Sundays. And in 2028 those days will all be Tuesdays. (If 2028 were not a leap year than they would be Mondays, but since 2028-04-04 is 366 days away from 2027-04-04 due to 2028-02-29 being added in, that's two more than the nearest multiple of 7 (364) and therefore the "key day" advances by 2).
As I said, I don't have a good mnemonic worked out yet for remembering which "key day of the week" goes with which year. But the human mind remembers trauma, and I will probably never forget that September 11, 2001 was a Tuesday. (I clearly remember arriving at work and being asked "Did you hear about the plane crashes?" — both had happened during my 30-minute walk to the office). Which gives me a fixed point I can always calculate from: September 4th, 2001 was a Tuesday, so September 5th was a Wednesday. That means 2001's key day of the week was a Wednesday, and then I can work forward from there: 2002 = Thu, 2003 Fri, 2004 Sun. Then I can add five days for every four years: 2008 Fri, 2012 Wed, 2016 Mon, 2020 Sat, 2024 Thu. Then 2025 Fri, 2026 Sat, and I'm back to current year.
That's not a great mnemomic, because it takes me quite a while to work back up to the current year from 2001. If anyone can think of a better mnemonic, please let me know.
hn111 2 hours ago [-]
Amazing mnemonics, thank you for sharing. Do you practice this as a mental exercise, or do you actually need to know the day of the week for a specific date that often?
rmunn 50 minutes ago [-]
Not that often, maybe one every couple of months. Usually when someone asks me "are you available at (time) on the 31st?" they actually expect me to pull out my phone and load the calendar app. But it's nice to be able to think "Hmmm, that's a Monday, I have that 9:30 AM meeting every Monday" and inform them "I can't do 10:00, how about 11:00?" without having to pull out my phone. (Then I pull out my phone to confirm that 11:00 AM works).
Where I have used it more often is when I am wondering "what day of the week will Christmas be this year?" or "what day of the week will my birthday fall on this year?". It's nice to be able to work those out mentally without pulling up a calendar app and tapping "next month" multiple times. But even that is a rare occurrence.
benjoffe 2 hours ago [-]
Thanks for sharing that.
I have a mobile-friendly web-app where you can practice this technique, just click the link in the page's nav: "Weekle - Weekday Guessing Game"
hn111 2 hours ago [-]
“All of that is math you can easily do in your head in seconds.”
It takes me way too long and I still have to use my fingers nowadays
Did not know it was called that; thanks for the link. And invented by John Conway (drawing on work by Charles Dodgson a.k.a. Lewis Carroll), no less? Man, the guy was a genius, wasn't he.
userbinator 3 hours ago [-]
I 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.
Joker_vD 2 minutes ago [-]
Well, maybe sometimes you need to format lots and lots of RFC 822 timestamps, or some other format that explicitly includes day-of-week.
gblargg 1 hours ago [-]
Not 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 1 hours ago [-]
It 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).
balgg 3 days ago [-]
My 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 2 days ago [-]
Thanks! 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.
The 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.
jph 6 hours ago [-]
Seconded. Ben, you have a great teaching style, thank you for sharing this.
WorseIsBetter 1 hours ago [-]
Doomsday rule for the brain, mul-add-shift for the machine. This article nails both. And those circle visualizations? Chef's kiss
alt227 2 hours ago [-]
Beautiful page. Great style, enjoyable writing, and excellent clear diagrams and equations. Well done!
xiaodai 4 hours ago [-]
look up table?
masklinn 54 minutes ago [-]
For 4 billion values?
jocelyner 6 hours ago [-]
[dead]
tobin1994 3 hours ago [-]
[dead]
Rendered at 09:08:55 GMT+0000 (Coordinated Universal Time) with Vercel.
[0]: https://www.youtube.com/watch?v=-dh7COSiB7g
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.
As I said, I don't have a good mnemonic worked out yet for remembering which "key day of the week" goes with which year. But the human mind remembers trauma, and I will probably never forget that September 11, 2001 was a Tuesday. (I clearly remember arriving at work and being asked "Did you hear about the plane crashes?" — both had happened during my 30-minute walk to the office). Which gives me a fixed point I can always calculate from: September 4th, 2001 was a Tuesday, so September 5th was a Wednesday. That means 2001's key day of the week was a Wednesday, and then I can work forward from there: 2002 = Thu, 2003 Fri, 2004 Sun. Then I can add five days for every four years: 2008 Fri, 2012 Wed, 2016 Mon, 2020 Sat, 2024 Thu. Then 2025 Fri, 2026 Sat, and I'm back to current year.
That's not a great mnemomic, because it takes me quite a while to work back up to the current year from 2001. If anyone can think of a better mnemonic, please let me know.
Where I have used it more often is when I am wondering "what day of the week will Christmas be this year?" or "what day of the week will my birthday fall on this year?". It's nice to be able to work those out mentally without pulling up a calendar app and tapping "next month" multiple times. But even that is a rare occurrence.
I have a mobile-friendly web-app where you can practice this technique, just click the link in the page's nav: "Weekle - Weekday Guessing Game"
It takes me way too long and I still have to use my fingers nowadays
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).
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.
You might also https://www.redblobgames.com/ and learn a lot from how they do their presentations.
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.