If it’s the fourth this must be Thursday

What’s the day of the week of [insert date here]? Do it in your head.

There’s some similarity between calculating the phase of the moon for a given date and calculating the day of the week for a given date. Also some differences.

Similarly, how one does it depends on whether one’s doing it in one’s head or on a computer. (And how one does it on a computer depends on the software environment. Modern software libraries tend to make calculating the day of the week a complete non-problem, for instance. Whereas if you were programming in FORTRAN in the 1970s, it was on you.)

Differently, though, in terms of accuracy. The phase of the moon varies continually and slowly enough that getting it right to within a day or so is probably good enough, at least if you’re doing a mental calculation; if more precision matters, you probably want to do it in software anyway. Whereas if you want to know if June 17 is a Monday, finding out it’s probably within a day or so of Monday isn’t likely to be good enough. For the day of the week, either it’s right or it isn’t.

Anyway, if you were to do it, how would you do it?

We’ll specify a date with century, year, month, and day numbers. The day number D is just the number from 1 to 31 denoting which day of the month it is. Now, for month numbers, M, because leap years make things so complicated, let’s use years that start on March 1 and end on February 28 or 29. Within a March-to-February year we can figure out the number of days between any two dates without having to worry about whether it’s a leap year or not. So instead of M running from 1 (January) through 12 (December) we’ll use 3 (March) through 14 (February). January and February will be regarded as belonging to the preceding calendar year. For March through December the year number Y is the last two digits of the year (AD) and the century number C is the preceding digits. For January and February, use the same Y and C as for the preceding March.

What about BC years? Forget ’em. We’re dealing with Gregorian calendar only here because otherwise, ow. You can develop formulas for Julian calendar and BC and whatever else on your own, if you insist.

Okay. For 10 Apr 1937 we have C = 19, Y = 37, M = 4, D = 10. For 2 Feb 1600 we have C = 15, Y = 99, M = 14, D = 2. Got it?

If you know (x+1) mod 7, the weekday (encoded as an integer from 0, for Sunday, through 6, for Saturday) of the 1st of the month, then the formula for any other day in that month is simple: (D) mod 7.

How do you get x? If every month were 30 days then it’d be (y + 30(M–3)) mod 7 = (y + 2+ 1) mod 7, and therefore the weekday of month M day D would be (y + 2+ 1 + D) mod 7, where (y + 1) mod 7 is the weekday of 1 Mar. But unfortunately it’s more complicated. “Thirty days hath September…” (Or do you know this trick? Make a fist with your left hand. You have four knuckles, and three valleys between the knuckles. Label the knuckles and valleys with the month names starting from January; that and March and May and July are knuckles, February and April and June are valleys. Continue on the right fist: August, October, and December are knuckles and September and November are valleys, with a knuckle and a valley left over. The knuckle months have 31 days, the valley months don’t. You can get “JMMJ AONx” tattooed on your knuckles, if you want a good conversation starter.)

1 Mar is day 1 of the year, or 0 days more than (3–3) x 30 + 1. 1 Apr is the 32nd day, or 1 day more than (4–3) x 30 + 1. 1 May is the 62nd day, or 1 day more than (5–3) x 30 + 1. And so on. The whole table for the number of days more than 30 x (M–3) + D days past the last day of the preceding year is:

March 0
April 1
May 1
June 2
July 2
August 3
September 4
October 4
November 5
December 5
January 6
February 7

So now if you know y, and you’ve memorized that table (call those numbers mM) then the formula becomes:

(y + 2M + 1 + mMD) mod 7

Memorizing that table’s a pain, but what else can you do? (Well, see below.) Note we can absorb additive constant values into y, so let’s just use

(y + 2M + mMD) mod 7

and just define y as whatever works.

Now, if you know the weekday of a date in some century, and if there were no leap years, then you could get the weekday for any date in that century just by using

(z + Y + 2MmMD) mod 7

because in any year the weekday for a given date is one later than it was the previous year. (365 mod 7 = 1.) Here z is, well, it’s what makes the formula come out right for the date you know.

But there are leap years, which add another 1 day shift from one year to the next. So we have to add 1 for each leap year from year 0 through Y–1, inclusive. Recall that Y = 0 is not a leap year, in any century, because by Y = 0 we mean 1 Mar of calendar year 0 through 28 Feb of calendar year 1. On the other hand Y = 3 is 1 Mar 03 through 29 Feb 04, and is a leap year! It won’t shift any days in that year but it’ll shift days starting in Y = 4. So — ignoring the century leap year rule for a moment — the number of leap years to take into account is just floor(Y/4). We have to add that number to our tally.

So for dates in that century we use:

(z + Y + floor(Y/4) + 2MmMD) mod 7

How about dates in the next century? Well, in most cases a century year (our Y = 99 year) is not a leap year so there are 24 leap years and 76 standard years in a century, or 24 x 2 + 76 x 1 mod 7 = 5 weekdays shift. If all centuries were like that it’d be

(z + 5CY + floor(Y/4) + 2MmM D) mod 7

I’m still calling the constant z, but it’s whatever makes things work.

But every 400 years we do have a leap year. So we have to add 1 day back in for every four centuries:

(z + 5C + floor(C/4) + Y + floor(Y/4) + 2MmMD) mod 7

Now we just need to determine z. Pick a day at random, oh let’s say Tuesday, 2 Jan 2018. Then it’s (remembering Y = 17 and M = 13 for January 2018)

(z + 5 x 20 + floor(20/4) + 17 + floor(17/4) + 2 x 13 + 7 + 2) mod 7

= (z + 2 + 5 + 3 + 4 + 5 + 6 + 2) mod 7

= (z + 6) mod 7

But this should equal 2 for Tuesday, so z = 3.

And now we can do any date. 17 Apr 2733 is:

(3 + 5 x 27 + floor(27/4) + 33  + floor(33/4) +  2 x 4 + 1 + 17) mod 7

= (3 + 2 + 6 + 5 + 1 + 1 + 1 + 3) mod 7 = 1 = Monday. Right, cal(1)?

$ cal 4 2733
     April 2733
Su Mo Tu We Th Fr Sa
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29


Oh, one more thing:

The red dots are the values of mM plotted versus M. The blue dots are mM+1. The line is y = 0.6M – 1.3, and it passes between each pair of dots. So that means instead of memorizing mM and using

(3 + 5C + floor(C/4) + Y + floor(Y/4) + 2MmMD) mod 7

if you’d rather you could use

(3 + 5C + floor(C/4) + Y + floor(Y/4) + 2M + floor((6M – 13)/10) + D) mod 7

So just memorize the first formula and mM, or memorize the second formula, and keep in mind you have to use Y and for the preceding year and add 12 to M if the month is January or February, and know your Gregorian calendar limits for the particular geographical location in question (remember, February 1712 had 30 days in Sweden), and you can figure out the weekday in your head!

… Either way, the lunar phase is easier.


Season to taste

Yesterday was the vernal equinox, or as Phil Plait calls it, the “March and not Vernal because of the Southern Hemisphere not quite equinox so let’s say it’s the quasinox.” And about damn time, too.

In the older post he links to, he discusses the fact that we (officially) use the solstices and equinoxes to mark the boundaries between the seasons, and that in some ways it makes more sense to do what’s more common in some countries and traditions, to regard the solstices and equinoxes as the midpoints of the seasons. So, for example, June 21 (or 20) would be the middle of summer (i.e. Midsummer; here and elsewhere, I’m speaking from a Northern Hemisphere-centric point of view; adjust as needed for the Southern Hemisphere), not the start of it: summer would run from about six weeks before that — early May — to six weeks after, early August. Likewise December 21 (or 22) would be the middle of winter.

That’s even in our own cultural heritage. If the vernal equinox were the central day of spring, then spring would begin about six weeks earlier — around February 2. And in our folklore, it does… if the groundhog doesn’t see his shadow.

From a weather point of view (and Plait emphasizes he’s not considering weather, but then he says to him it makes more sense for the Sun to hit its peak at summer’s midpoint, but why that makes more sense if not for weather reasons, he doesn’t make clear) that seems, on the face of it, much more sensible. You think of summer as the hottest time of the year, and the reason it’s hot is because as the Sun’s path moves northwards, the angle of incidence gets smaller and smaller, and the amount of solar radiation per second striking a plane tangent to the Earth’s surface gets larger and larger. The angle of incidence is smallest, and the amount of solar radiation is the largest, at the summer solstice.

But, continuing from a weather point of view, the summer solstice is not the hottest day of the year (on average). Just as it takes a turkey several hours to cook after you start heating it, it takes time for the atmosphere, the water bodies, and the land to warm up. How long that takes, and therefore when peak temperature occurs, I would guess depends on a number of factors including latitude, altitude, and proximity to large water bodies and to water currents (e.g. the Gulf Stream). Just looking at a number of U.S. cities I find Phoenix, AZ seems to reach peak temperature about July 7, while Los Angeles apparently doesn’t start cooling off until around August 20. (But Los Angeles’s minimum temperature seems to be in mid-December, before the solstice! Same for Denver. Very weird, I don’t understand that.) Mid to late July, say around July 21 — a month after the summer solstice — seems to be fairly typical, though, and early to mid January for the coldest date. Center the seasons around those, rounded to a convenient nearby date, and you’d have spring from about March 1 to June 1, summer from June 1 to September 1, fall from September 1 to December 1, and winter from December 1 to March 1. Which in fact is the definition meteorologists use.