# Calendar Algorithms

Given a date in history, what day of the week was it?

First, some terminology. A sidereal year is the time is takes for the Earth to make 1 revolution around the sun relative to the fixed stars. It is approximately 365.25636 days.

The tropical year is the time it takes for the mean tropical longitude of the Sun to increase by 360 degrees. (Due to the increase in the Earth's precessional rate, the length of the tropical year decreases over time. A tropical year is approximately 365.24220 days.

The discrepancy between the sidereal year and the tropical year results from the precession of the Earth's axis of rotation: The Earth "wobbles" as it rotates. If no allowances were made for this, the seasons would occur at a slightly different time every year.

The Egyptian calendar had 365 days in a year. In 45 B.C., the Julian calendar was introduced. It had 365.25 days in a year, the year began in January (there being 12 months instead of the previous 10), and there was a leap year every 4-th year. This calendar loses approximately 1 day every 128 years.

By 1582, the calendar was nearly 11 days behind the calendar of 325 A.D. (Nicea). The vernal equinox, which now occurs around March 21, is the instant during spring when the sun "crosses" the Earth's equatorial plane. The vernal equinox had occurred around March 21 in the year 325; by the year 1582, it was occurring around March 11. The Gregorian calendar was introduced by Pope Gregory XIII in 1582. The ten days from October 4 through October 14 were dropped. (In other words, the day after October 4 was October 15.) The present leap year algorithm was introduced: A year divisible by 4 is a leap year, unless it's also divisible by 100, though years divisible by 400 are still leap years. Relative to the tropical year, this calendar loses around 1 day every 3300 years.

I'm going to account for the change from the Julian calendar to the Gregorian calendar later. First, I'll work out the day of the week for the Gregorian calendar in several steps.

Step 1 Find the day of the week of the first day of a given year.

It is convenient to have years "begin" on March 1. But so I don't get totally confused, whenever I write a date it will be understood to be the "standard" date. If I need to distinguish between a new date and a standard date, I'll use appropriate qualifiers.

I'll also use the following notation:

Example. September 13, 1857: , (since March is 1), , .

January 3, 1954: , , , . Note that 1953 has been redefined to run from the old March 1, 1953 to the old February 28, 1954.

The days of the week will be numbered beginning with Sunday:

Remember that March 1 is now the first day of the year. Let

Example. In 1994, March 1 was a Tuesday, so .

If you know one value of , you can use it to get others. Note that

That is, increases by 1 over a normal year and by 2 over a leap year. So suppose I knew . Then for ,

The first term accounts for the one day contributed by every year, while the second term adds the extra day for each leap year. Note that since the year begins on March 1, this has to be interpreted correctly: the old 1992 was a leap year, but its extra day is contributed by the new 1991 (which extends through the old March 1, 1992).

There is a similar formula using any year besides 1600.

Example. Consider March 1, 1997. , and there is one intervening leap year. So

So March 1, 1997 will be a Saturday.

I'll go back to 1600 as my reference. I want to know how many leap years there were between 1600 and the year N, where . It's easy to count using the greatest integer function:

Integers can be moved out of the greatest integer function, so I get

Now , so this becomes

and C are integers, and may be moved out of the greatest integer function. Since , , and . Moreover, , and a little thought shows that this implies . So I now have

Dump this into the formula for :

I can determine by using :

Thus, the formula is

Example. Consider March 1, 1776. , , so

It was a Friday.

Step 2 Find the day of the week for the first day of an {\it arbitrary} month in an arbitrary year.

Since February is the last month by my month convention, I only need to worry about months with 30 or 31 days in counting from March 1. These are months 1 through 11. Notice that

Thus, a month with 30 days causes the day of the week of the first day to shift by 2, while a month with 31 days causes the day of the week of the first day to shift by 3. I need a formula to "correct" by the right amount for . The following formula works:

That is, at the beginning of month m, the day of the week has shifted by from .

Agustin Suarez pointed out that the numbers "2.6" and "0.2" can be found by fitting a line to the data. This method is called Zeller's congruence; it was discovered by Christian Zeller in 1882.

Example. March 1, 1776 was a Friday, i.e. . Then for April 1, 1776, (April), and

So April 1, 1776 was a Monday. And February 1, 1777 gives

It was a Saturday. (Note that the old February 1, 1777 is considered part of 1776 by my convention, so .)

Step 3 Find the day of the week for an arbitrary day in history (by the Gregorian calendar).

Consider the day k of the month m in the year . The formula from Step 2 gives the day of the week for the first day of the month, i.e. for . The shift from 1 to an arbitrary k is just :

Example. January 24, 1988 was a Super Bowl Sunday ... uh, was it really a Sunday?

Here , , , . So

It works! --- it was a Sunday! Isn't math wonderful!

Finally, I'll explain the correction you need for Julian dates. Please note that this is still a simplification, because different countries converted to the Gregorian calendar at different times. I'll only do the case for the 1582 correction. The procedure is:

1. Add 10 days to the Julian date.

2. Subtract one day for each century not divisible by 400 between the Julian date and October 15, 1582.

Here is the rationale. Without worrying about the centuries, the Julian and Gregorian calendars would be 10 days apart due to the 10 days that Pope Gregory removed: . But the Julian calendar also gives a leap day to century years such as 1500, whereas so 1500 is not a Gregorian leap year. Here's how this looks in a particular case:

Note that 1500 was a Julian leap year, but not a Gregorian leap year. As I move backwards through the leap day, the calendars go from being 10 days apart to being 9 days apart. That is, the Julian calendar gains a day on the Gregorian calendar for each such century leap year --- which explains the correction above.

Example. April 15, 1452 (the birthday of Leonardo da Vinci) This is prior to 1582, so I'll take it to be a Julian date. I add 10 days, then subtract one day for the single century date (1500) between 1452 and 1582 which is not divisible by 400. The corrected date is April 24, 1452. Thus, , , , and :

It was a Saturday.