Calendar formula

7-10-2011

How to compute the day of the week for any date in the Gregorian calendar

Contents

Introduction

The Gregorian calendar was introduced in 1582. It corrected the old Julian calendar, which was based on the wrong assumption that a year has exactly 365.25 days, adding a leap year every 4 years.


A year divisible by 4 is a leap year, but every 100 years (in a 'century year'), there is no leap year, but every 400 years, the century year (divisible by 400) is a leap year again (thus 2000 was a leap year and most people living now probably have no idea about the century rule).


Auxiliary tables

Table 1: centuries

Table 2: months

This will be called the month number.

Table 3: weekdays

Algorithm

Step 1 Take the century modulo 4, and take the value from Table 1 above

Step 2a If the date is not in a leap year, take the last two digits of the last leap year before it, multiply by 1.25 and add the difference between the required year and that leap year plus 1.

Step 2b If the date is in a leap year, multiply that by 1.25, and add 1 for March or later.

Step 3 Add the values from step 1 and 2, this will be called the year number.

Step 4 Add the value from Table 2 for the month and add the day.

Step 5 Compute the result of step 3 modulo 7

Note: x modulo y is the remainder when dividing by 7. For example, 7 modulo 3 is 1, because you can subtract 3 twice from 7.

In other words, it is the smallest nonnegative number obtained by subtracting a multiple of y from x. 

Step 6 Look up the day in Table 3.

Example

 

24 June 1869

Step 1 gives 2

Step 2a: 68 * 1.25 + 1 + 1 = 87

Step 3 gives 2 + 87 = 89

Step 4 gives 89 + 4 + 24 = 117

Step 5: 117 modulo 7 = 5

Step 6: Thursday

Why does it work this way and how did I find this formula?

When I was 16, I had a school diary with an 'everlasting calendar' that looked somewhat like this table, though it was set up in a simpler way (covering only two centuries).

By analyzing the structure of the numbers I came to the following insights (only applicable to the Gregorian calendar):

The calendar repeats every 400 years.

A non-leap year has 365 days, which is 7 times 52 plus 1, which means that the calendar of the next year will be shifted by one. After a leap year, the calendar moves ahead by 2 days. Thus after 4 years, the calendar has moved 5 ahead. This is where the factor 1.25 comes from.

 

The formula has to be gauged somewhere, it is obvious to set January at 0. Then because January has 31 days, which is 3 more than 4 weeks, the February calendar is shifted three days further. Hence the value 3 for February. As february is exactly 4 weeks, March has the same calendar. The subsequent values for the months can be derived the same way.

 

As there are 7 days which repeat forever, it is obvious to use modulo calculation.

 

Tricks

To increase efficiency, there are lots of tricks you can apply.

Instead of adding e.g. 5, you can also just subtract 2, as long as the number stays positive.

In most cases, the century is either the 20th or 21st giving just 0 or -1. For years between 1996 and the current year, I just know the year number. You might prefer to memorise a much larger set of year numbers, this depends on your own preference (I am inclined to just derive formulas rather than memorise the result as it gives me more confidence, but other people will be different). When you happen to know some year (say 1945, which often appears because of the war), then the year number for 1949 has to be 5 further.

You will find that it is actually more efficient to compute modulo whenever this is convenient (and possibly several times) rather than at the end. For example, if you know that 14 * 7 = 84, in the example above, after step 2 you have 89, and instead of continuing with that you just subtract 84 to get 5. This way you work with much smaller numbers which is easier. Then 24 June adds nothing, as June has month number 4 and 24 + 4 is divisible by 7, so you are left with 5 = Thursday.

In general keep in mind that any multiple of 7 can be omitted. With experience, you will probably need less than a second to compute the modulo 7 for any number up to 140.

Also nice to know is that the calendar repeats every 28 years, that the calendar of a year after a leap year is encountered again after 6 years, and for any other non-leap year it repeats after 11 years.

(All this assuming there is no exceptional century year in between).

 

For better understanding, please try to figure out yourself why this is the case (at least for the 28 year rule).

Century years

I recommend to simply memorize the year numbers for the century years:

1700 5

1800 3

1900 1

2000 6/0

Here 6 is the value for January and February and 0 for the later months.

Obviously, this holds for years modulo 400.

Then you don't need to worry about e.g. which century to take or whether to take an earlier leap year.

 

Marcel Monterie