3-instruction weekday calculation beats compiler output

A faster way to calculate the day of the week

3-instruction weekday calculation beats compiler output

Converting a day-count to the day of the week sounds trivial, but it's surprisingly complex. This article presents a range of really fast functions for the task, tuned for different use cases. The fastest algorithms achieve latency of just a single multiplication plus two cycles, and one surprising result shows that ISO format (1–7) can be computed with the same instructions as Unix format (0–6) with zero speed penalty. The techniques generalize to other Mersenne divisors like 24 and 60, and new fast modulus methods are introduced.

A surprising result is presented: the weekday can be computed in ISO format ([1‥7] instead of [0‥6]), with the exact same instructions, just with tweaked constants (and zero speed penalty).
  1. rmunn

    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 al […]

  2. balgg

    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

More from this day

2026-08-20