On 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.
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...
It 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.
It’s 2569 here in Thailand. Used on basically everything official and often more as on many sites, apps, etc, switching the language between English and Thai will alternate between the Thai year and Georgian year.
My partner's place of employment has a fiscal year ending June 30th and a use-it-or-lose-it vacation policy. We are hikers who live in the northeastern United States, which means we're inevitably planning a backpacking trip to use up her remaining vacation at the height of black fly[0] season.
We did a three week thru-hike of the Long Trail in 2022. We had shelters almost entirely to ourselves north of the AT/LT split at the Inn[1] because nobody hikes the LT in June/July if they can avoid it.
I think people save them up towards the end of whatever year just because you aren't sure if you're going to need them for medical leave or a sudden family trip or something. As you get close to the end you get more certain that you won't use them on something else and can plan a block trip
Even on 1), the first day of the week being Sunday (US) and Monday (Europe) screws me up constantly, as someone who is in both worlds now.
I often pick the wrong date for meetings because I'm assuming next Monday will be in the first column, not the second. As dumb and simple as it is, when something has been drilled into you since about age 3 its tough to get in the habit of double checking it when it starts becoming inconsistent.
This is one of the most annoying ones to me. I recently almost booked a pair of ~2000 euro return flights for the completely wrong dates because the booking site decided to (despite knowing our location and correctly setting currency, currency formatting and language) show it's date picker starting with Sundays. Only caught when I did a last sanity check before I clicked the pay button.
That’s illuminating. I was born and have lived in the U.S. my entire life. If you asked me what the first day of the week was, I would immediately respond with “Monday”.
I’m going to mull this one over a bit more to figure out where my social education went wrong!
> The end of the end of the week cannot be the beginning of the week.
Of course it can. The word “weekend” just refers to the block of free time starting at the end of the working week. It doesn’t have any direct relation to the way weekdays are laid out on a calendar. The word was invented in England at a time when the English still considered Sunday the first day of the week, a hundred years before ISO 8601 moved Monday to the start for commercial reasons (as described in Annex A).
When a public holiday falls on Monday, my friends and I call that weekend a “three‐day weekend” or a “long weekend,” even though the Monday falls well within what a strict ISO 8601er would consider the next week. What makes the weekend the weekend is when the weekend starts—the end of the work week—not when the weekend ends.
Dictionary.com, Merriam-Webster, and Cambridge all define the weekend as some derivation of "the end of the week".
I'm also not sure how the various definitions and historical context has anything to do with the claim that the end cannot be the beginning. If the end were the beginning that particular day would belong to two different weeks, but that's nonsense. The last day of the prior week and the first day of the current week are two different days, therefore:
> The end of the end of the week cannot be the beginning of the week.
The weekend has always been innately tied to the work week specifically, both historically and in current usage.
> Dictionary.com
Has as the third listed definition: “any two-day period taken or given regularly as a weekly rest period from one's work.” And has a whole paragraph describing usage, including: “More strictly speaking, the weekend is thought to consist of Saturday and Sunday (often regardless of whether the calendar week is considered to begin on Sunday or Monday). In practical terms, the weekend is typically considered to be the period between the end of the workweek (or school week) and the beginning of a new one—which is why most people consider their weekend to start on Friday night, after work or school.”
> Merriam-Webster
“the end of the week; the period between the close of one work or school week and the start of the next”
> Cambridge
“Saturday and Sunday, or Friday evening until Sunday night; the part of the week in which many people living in the West do not go to work”
Every dictionary you cited agrees that the weekend is or can be defined in relation to the work week!
> If the end were the beginning that particular day would belong to two different weeks, but that's nonsense.
Millions of people living in cultures where Sunday is the first day of the week have no problem with the concept of a single weekend consisting of a Saturday from one week and a Sunday from the next. There’s no logical contradiction. “Monday as the beginning of the week is the only logically defensible position if you support the weekend” is a false statement, because a single weekend is not required to consist solely of days from the same week.
You know, when you speak language where the word for Tuesday is literally "Seconder" and the word for Friday is "Fifter", it's really difficult to accept that starting to number the days of the week from Sunday is a sensible thing.
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.
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.
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?
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.
Basically the following days have the same day of the week: Jan 3 or 4 on a leap year, last day of February, all the even pairs day / month days after that (4/4, 6/6, 8/8, 10/10, 12/12), 7/11 and 11/7, 9/5 and 5/9.
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!
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.
I don't know what's wrong with me, but I can't do mental calculations with dates because I always think I'm off-by-one, no matter what.
I prefer to "move vertically" in the month. Knowing that 1=8=15=22=29, using prlin's example for December 25 (sibling comment), I'd go like:
"12 is Saturday, 15 is Tuesday, 22 is also Tuesday, so 25 is Friday."
So, starting from key date (12), moving to "the 1 column" (15), moving up or down to the closest/easiest date (22), and then finishing from there.
Slower for sure, but easier for me to not lose track. And now having a known day in every month thanks to rmunn's mnemonic, is going to be a huge help.
I'm curious to understand what kind of life you live, or what you work with, when this information becomes so important that you need to be able to calculate it on whim, and fast too?
Personally whenever someone asks me if I'm available on some date, or what I'm doing at a specific day or whatever, almost never does it matter what day of the week is, it's more about what things I have planned, and for that I have to open the calendar regardless as events beyond 1+ week horizon is quickly filled into my "Blank" cabinet in the brain.
Not saying it isn't useful in the sense that avoiding these tiny hassles of "open calendar, find the date, look up what weekday" isn't worth it, I've unexplicitly trained myself to be ambidextrous for no real purpose except "that's neat", so I'd understand it, I'm mostly curious about actual usefulness of this in other's daily life, as seemingly my own looks very different from the ones where this'd be useful.
Like I said in answer to a similar question somewhere else in this thread, I rarely actually need to use this. But there have been times when it's come in handy, such as when I'm away from my computer and someone calls me on the phone to ask if I want to meet them for lunch on the 13th. I rarely remember my appointments by day of the month, but I do usually remember them by day of the week, i.e. I think "the 13th... (two seconds of mental math)... that's a Thursday. Right, I already made plans to have lunch with John Smith on Thursday next week." And then I can tell them "Sorry, I'm busy on the 13th, but I think I'm free on the 14th, let me check." And then I switch over to the calendar app, verify the 14th is free, and switch back to the phone app to confirm that I can meet them on Friday the 14th.
That's how I end up using this most often. (And "most often" is still just every couple of months, mind). I still need to use the calendar app to record my appointments, but it sometimes lets me give people a "no, I'm busy that day, how about (other day)?" response with less of a conversational pause.
So like you, I don't find it all that useful, but it still is nice to have from time to time. And I also just find it fun sometimes to know that the 13th of the month = Thursday without having to look anything up.
I think I may prefer that one, because it also allows me to figure out people’s birthdays. The key insight to making that one faster is to realise you can precalculate and memorise the year value for the year you’re currently in. Granted, having to memorise 12 magic numbers for the months maybe makes that one harder, but for some reason I immediately memorised the table upon looking at it once.
To answer the question: I don’t really need to be able to do this, but I like it. It’s a fun, short, mental exercise. I even set up a daily reminder to quiz me on a random date every day.
But it is practically useful, and it surprised me in how often I use it. Someone tells me of an event that’s going to be on a certain date and without breaking conversation I can figure out what day of the week it’ll be. I still think I’m fairly slow (I tend to double-check myself), but can still do it consistently slightly faster than someone who needs to take their phone out.
Also, on a couple of occasions it has happened that as a group we needed to know a day of the week, someone checked it on their phone and made a mistake and I was able to catch it.
> ... have to remember that it's one day less in non-leap-years ...
How about just remember January 2nd but for the following year as key days. i.e. if you consider January and February 2027 to be part of 2026's "key days year", then you can forget about whether it's a leap year or not (though you still have to calculate what the key day is from year to year).
[Aside - it would be simpler if the leap day were the 32/Dec instead of 29/Feb!]
Noting also that Jan & Feb (of next year) are the same as May & June (of this year), so you get 2/Jan/Y+1 and 6/Feb/Y+1.
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.
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
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.
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.
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).
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...
Not 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/
On 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 , and generally persian-related cultural things: 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_ca...
It 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.
The French tried this: https://en.wikipedia.org/wiki/French_Republican_calendar
I wonder what they would have thought of swatch internet time.
It's the year 2083 in Nepal right now.
It’s 2569 here in Thailand. Used on basically everything official and often more as on many sites, apps, etc, switching the language between English and Thai will alternate between the Thai year and Georgian year.
My partner's place of employment has a fiscal year ending June 30th and a use-it-or-lose-it vacation policy. We are hikers who live in the northeastern United States, which means we're inevitably planning a backpacking trip to use up her remaining vacation at the height of black fly[0] season.
We did a three week thru-hike of the Long Trail in 2022. We had shelters almost entirely to ourselves north of the AT/LT split at the Inn[1] because nobody hikes the LT in June/July if they can avoid it.
[0] https://en.wikipedia.org/wiki/Black_fly
[1] https://innatlongtrail.com/
Does the policy somehow preclude you from using the remaining days before the beginning of June or end of May?
Seems easy enough to dodge if it's not the case that the relative privacy outweighs the flys.
I think people save them up towards the end of whatever year just because you aren't sure if you're going to need them for medical leave or a sudden family trip or something. As you get close to the end you get more certain that you won't use them on something else and can plan a block trip
Even on 1), the first day of the week being Sunday (US) and Monday (Europe) screws me up constantly, as someone who is in both worlds now.
I often pick the wrong date for meetings because I'm assuming next Monday will be in the first column, not the second. As dumb and simple as it is, when something has been drilled into you since about age 3 its tough to get in the habit of double checking it when it starts becoming inconsistent.
This is one of the most annoying ones to me. I recently almost booked a pair of ~2000 euro return flights for the completely wrong dates because the booking site decided to (despite knowing our location and correctly setting currency, currency formatting and language) show it's date picker starting with Sundays. Only caught when I did a last sanity check before I clicked the pay button.
That’s illuminating. I was born and have lived in the U.S. my entire life. If you asked me what the first day of the week was, I would immediately respond with “Monday”.
I’m going to mull this one over a bit more to figure out where my social education went wrong!
Im in the US and I’d say the week starts on monday but calendar interfaces display a sunday first.
https://www.timeanddate.com/calendar/?year=2026&country=9
Toggle the calendars here and you’ll see it.
Oh, I agree that’s the case, and I’ve noticed it over the years, but never thought deeply on it other than “that’s an annoying preset”
Monday as the beginning of the week is the only logically defensible position if you support the weekend.
There is only one weekend. One end of the week. It goes at the end as it is definitionally required to do.
If Sunday is the end of the weekend, it is the end of the end of the week.
The end of the end of the week cannot be the beginning of the week.
Sunday comes after Saturday comes after Friday...comes after Monday.
Therefore, Monday is the beginning of the week.
QED
The week has two ends. Maybe the people putting Sunday at the start are just trying to cover both options.
So it should be called weekends?
That's my standard response. "If Sunday's at the beginning, then there are two weekends per week."
Logically, the weekend days belong together on a calendar but that's not how US calendars are typically laid out.
I think the weekend could cover the beginning and the end, just like a bookend can be at the beginning or end of the book shelf
My shelves all have two bookends. If they only had one, the books would fall off the end without a bookend.
But, this is simply mataphorical reasoning. One cannot conflate weeks with bookshelves for many reasons.
Otherwise, the weekend should have been called weekedge
> The end of the end of the week cannot be the beginning of the week.
Of course it can. The word “weekend” just refers to the block of free time starting at the end of the working week. It doesn’t have any direct relation to the way weekdays are laid out on a calendar. The word was invented in England at a time when the English still considered Sunday the first day of the week, a hundred years before ISO 8601 moved Monday to the start for commercial reasons (as described in Annex A).
When a public holiday falls on Monday, my friends and I call that weekend a “three‐day weekend” or a “long weekend,” even though the Monday falls well within what a strict ISO 8601er would consider the next week. What makes the weekend the weekend is when the weekend starts—the end of the work week—not when the weekend ends.
You are conflating the work week with the week.
ISO is but one standard.
Dictionary.com, Merriam-Webster, and Cambridge all define the weekend as some derivation of "the end of the week".
I'm also not sure how the various definitions and historical context has anything to do with the claim that the end cannot be the beginning. If the end were the beginning that particular day would belong to two different weeks, but that's nonsense. The last day of the prior week and the first day of the current week are two different days, therefore:
> The end of the end of the week cannot be the beginning of the week.
> You are conflating the work week with the week.
The weekend has always been innately tied to the work week specifically, both historically and in current usage.
> Dictionary.com
Has as the third listed definition: “any two-day period taken or given regularly as a weekly rest period from one's work.” And has a whole paragraph describing usage, including: “More strictly speaking, the weekend is thought to consist of Saturday and Sunday (often regardless of whether the calendar week is considered to begin on Sunday or Monday). In practical terms, the weekend is typically considered to be the period between the end of the workweek (or school week) and the beginning of a new one—which is why most people consider their weekend to start on Friday night, after work or school.”
> Merriam-Webster
“the end of the week; the period between the close of one work or school week and the start of the next”
> Cambridge
“Saturday and Sunday, or Friday evening until Sunday night; the part of the week in which many people living in the West do not go to work”
Every dictionary you cited agrees that the weekend is or can be defined in relation to the work week!
> If the end were the beginning that particular day would belong to two different weeks, but that's nonsense.
Millions of people living in cultures where Sunday is the first day of the week have no problem with the concept of a single weekend consisting of a Saturday from one week and a Sunday from the next. There’s no logical contradiction. “Monday as the beginning of the week is the only logically defensible position if you support the weekend” is a false statement, because a single weekend is not required to consist solely of days from the same week.
1st joke https://youtube.com/shorts/3p2Apuhj40E
You know, when you speak language where the word for Tuesday is literally "Seconder" and the word for Friday is "Fifter", it's really difficult to accept that starting to number the days of the week from Sunday is a sensible thing.
If you’re referring to Slavic languages, they’re counted from Sunday.
• Sunday: “no work”
• Monday: “[first] after Sunday”
• Tuesday: “second [after Sunday]”
• Wednesday: “middle”
• Thursday: “fourth [after Sunday]”
• Friday: “fifth [after Sunday]”
• Saturday: “sabbath”
Treating the work week as the start of the calendar week is a 20th century invention, as is the five‐day work week. Historically, Slavic calendars began on Sunday: https://www.russianhistorymuseum.org/wp-content/uploads/2025...
https://www.anycalendar.io/
The hard part isn't converting dates, it's remembering which calendar is socially relevant in which context
Respect to 2, don't you just look up at the start of the (gregorian) year when stuff is?
I mostly do that in my country just to check when the holy friday holiday is, a few saint holidays, a couple of celebrations for the virgin mary, etc.
Like, I know Easter follows a weird lunar calendar but I don't really calculate it.
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.
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.
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?
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.
For figuring the doomsday of a particular year, you can use the Odd+11 rule.
Let X be the last two digits of a year.
If X is odd, add 11.
Divide X by 2.
If X is odd, add 11.
Let Y = 7 − (X mod 7).
Count forward Y days from that century's anchor day to get the year's doomsday.
https://arxiv.org/pdf/1010.0765
https://en.wikipedia.org/wiki/Doomsday_rule
Basically the following days have the same day of the week: Jan 3 or 4 on a leap year, last day of February, all the even pairs day / month days after that (4/4, 6/6, 8/8, 10/10, 12/12), 7/11 and 11/7, 9/5 and 5/9.
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!
More info: https://en.wikipedia.org/wiki/Doomsday_rule
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.
“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
Some examples are easier than others, like the mentioned 12/25. 12/12 is Saturday -> 25-12 is 13 more days -> same day as 1 before 12/12 -> Friday.
I don't know what's wrong with me, but I can't do mental calculations with dates because I always think I'm off-by-one, no matter what.
I prefer to "move vertically" in the month. Knowing that 1=8=15=22=29, using prlin's example for December 25 (sibling comment), I'd go like:
"12 is Saturday, 15 is Tuesday, 22 is also Tuesday, so 25 is Friday."
So, starting from key date (12), moving to "the 1 column" (15), moving up or down to the closest/easiest date (22), and then finishing from there.
Slower for sure, but easier for me to not lose track. And now having a known day in every month thanks to rmunn's mnemonic, is going to be a huge help.
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"
I'm curious to understand what kind of life you live, or what you work with, when this information becomes so important that you need to be able to calculate it on whim, and fast too?
Personally whenever someone asks me if I'm available on some date, or what I'm doing at a specific day or whatever, almost never does it matter what day of the week is, it's more about what things I have planned, and for that I have to open the calendar regardless as events beyond 1+ week horizon is quickly filled into my "Blank" cabinet in the brain.
Not saying it isn't useful in the sense that avoiding these tiny hassles of "open calendar, find the date, look up what weekday" isn't worth it, I've unexplicitly trained myself to be ambidextrous for no real purpose except "that's neat", so I'd understand it, I'm mostly curious about actual usefulness of this in other's daily life, as seemingly my own looks very different from the ones where this'd be useful.
I reckon it makes for a pretty neat party trick, I guess
Like I said in answer to a similar question somewhere else in this thread, I rarely actually need to use this. But there have been times when it's come in handy, such as when I'm away from my computer and someone calls me on the phone to ask if I want to meet them for lunch on the 13th. I rarely remember my appointments by day of the month, but I do usually remember them by day of the week, i.e. I think "the 13th... (two seconds of mental math)... that's a Thursday. Right, I already made plans to have lunch with John Smith on Thursday next week." And then I can tell them "Sorry, I'm busy on the 13th, but I think I'm free on the 14th, let me check." And then I switch over to the calendar app, verify the 14th is free, and switch back to the phone app to confirm that I can meet them on Friday the 14th.
That's how I end up using this most often. (And "most often" is still just every couple of months, mind). I still need to use the calendar app to record my appointments, but it sometimes lets me give people a "no, I'm busy that day, how about (other day)?" response with less of a conversational pause.
So like you, I don't find it all that useful, but it still is nice to have from time to time. And I also just find it fun sometimes to know that the 13th of the month = Thursday without having to look anything up.
I’m not the person you asked and the technique I use is different (though also from an HN post).
https://news.ycombinator.com/item?id=40464664
I think I may prefer that one, because it also allows me to figure out people’s birthdays. The key insight to making that one faster is to realise you can precalculate and memorise the year value for the year you’re currently in. Granted, having to memorise 12 magic numbers for the months maybe makes that one harder, but for some reason I immediately memorised the table upon looking at it once.
To answer the question: I don’t really need to be able to do this, but I like it. It’s a fun, short, mental exercise. I even set up a daily reminder to quiz me on a random date every day.
But it is practically useful, and it surprised me in how often I use it. Someone tells me of an event that’s going to be on a certain date and without breaking conversation I can figure out what day of the week it’ll be. I still think I’m fairly slow (I tend to double-check myself), but can still do it consistently slightly faster than someone who needs to take their phone out.
Also, on a couple of occasions it has happened that as a group we needed to know a day of the week, someone checked it on their phone and made a mistake and I was able to catch it.
> ... have to remember that it's one day less in non-leap-years ...
How about just remember January 2nd but for the following year as key days. i.e. if you consider January and February 2027 to be part of 2026's "key days year", then you can forget about whether it's a leap year or not (though you still have to calculate what the key day is from year to year).
[Aside - it would be simpler if the leap day were the 32/Dec instead of 29/Feb!]
Noting also that Jan & Feb (of next year) are the same as May & June (of this year), so you get 2/Jan/Y+1 and 6/Feb/Y+1.
> Aside - it would be simpler if the leap day were the 32/Dec instead of 29/Feb!
Well, February did use to be the last month of the year. (And whoever messed it up did, in fact, get stabbed.)
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.
Well, maybe sometimes you need to format lots and lots of RFC 822 timestamps, or some other format that explicitly includes day-of-week.
finance, many yield curve calculations have a lot of date arithmetic in their hot paths
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
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.
Thanks for putting in the effort.
You might also https://www.redblobgames.com/ and learn a lot from how they do their presentations.
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.
Seconded. Ben, you have a great teaching style, thank you for sharing this.
I miss when "using the web" meant enhancing a document rather than replacing it with 4MB of JS and a dashboard
This is exactly the kind of optimization rabbit hole I enjoy
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.
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).
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...
There was this[0] famous answer on University Challenge, but it's a pretty simple calculation.
[0]: https://www.youtube.com/watch?v=-dh7COSiB7g
"We need Jungle I'm afraid"
https://www.youtube.com/shorts/8jZUTPPf6do
See the Doomsday algorithm, discussed elsewhere on this thread. https://en.wikipedia.org/wiki/Doomsday_rule
I am done with time, extra seconds, missing days, time zones, daylight savings, relativity.
Distance from the big bang from now on.
Sounds like you also measure from Kelvin
Does your formula/code take into account the gravimetric degradation brought about by EP260321a?
So are there no discontinuities for weekday calculation? Never 2 Sundays in a row or a maybe even a Monday skipped?
Not 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/
If 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.
Nice! And what a beautiful website!
Below the surface it's all the division with constant trick.
Doomsday rule for the brain, mul-add-shift for the machine. This article nails both. And those circle visualizations? Chef's kiss
Beautiful page. Great style, enjoyable writing, and excellent clear diagrams and equations. Well done!
Earlier, from the same author https://news.ycombinator.com/item?id=46020193
if day = "friday" then output = :)
else output = :(
look up table?
For 4 billion values?
That would be 10.9 million years. Could probably get away with a slightly smaller look up table.