Monday, July 7, 2014

The day-of-week for any day this year (and this century)

I came up with this approach to figure out the day-of-week for any day during the current year:

  • Each year, remember the weekday of first day of the year, eg. Wed in 2014  (See below for a formula for this in the 21st century.)
  • Memorize this set of numbers, one for each month: 155274263153 ... which is the date of the first time that day occurs each month

You can probably get it from there... so here's an example: what day is Dec 25 this year?
  • December is month #12, so take the last digit in that set of numbers: 3
  • That means the 3rd is the first time that Wed occurs this December.
  • Add multiples of 7 to get to other Wed that are close: 
    • +7=10
    • +14=17
    • +21 = 24
  • Since Dec 24 is on Wed, Dec 25 must be on Thurs
  • The exception is leap years, and I typically subtract one from the first day in January and February.
This makes it pretty easy, because that first weekday only changes each year and I can remember that day for the whole year.

Now, for the majority of my lifetime (if things don't go as I hope), it's actually pretty easy to find the first day of the year for any year:
  • Take the year without the 2000 part, eg. 14 for 2014
  • Calculate (y + y div 4) mod 7
    • or (y mod 7 + y div 4) mod 7
    • or (y mod 7 + (y div 4) mod 7) mod 7
  • 0=Sun, 1=Mon, etc.
    • except for on leap years (divisible by 4) subtract 1 day for Jan-Feb
Pretty easy to remember for us mathematicians... it starts with Sunday, and then moves one day each year with an extra adjustment on leap years.

The rules get really complicated for previous or future centuries because we don't have leap-years every 100 years but then we do have them every 400 years.  But, like I say, this works for my foreseeable future, in 99.9% of the times I ever need to find a day.

I haven't written this up before because I checked years ago and found people had already written about this algorithm, but I think it's worth mentioning now because it's so easy for the vast majority of cases people use for a while to come.  BTW, if I want a comprehensive algorithm, I'll probably try Conway's Doomsday Algorithm.

No comments: