Hi there Martin,

I've just been reading your home page on the Internet and realised that there's no time like the present to get round to writing the instructions I promised a long time ago for working out the day of the week for a particular date. So here goes. Having read these notes again I've realised that they might look a little daunting at first but don't be put off by that, the algorithm is quite easy really.

I remember working this algorithm out one day in 1978 when I had an idle moment (well, afternoon) sitting in the library at St Joseph's College in west Belfast.

Having worked out an algorithm I quickly realised that it would fail as soon as it reached 1979. This lead to a rethink and the following basis of an algorithm was produced:

The 31 Oct 1999 is a Sunday (needn't go into the verification of this now - 'trust me'). Therefore, to calculate the associated day for any date, it is necessary to work out by how much the date differs from 31 Oct 1999 and therefore by how many days it differs from a Sunday.

The above date and day were chosen very carefully as will be explained later.

And now for some other background information which is worth knowing before continuing:

Theoretically there are two distinctly different phenomena that need to be reconciled in order to calculate the day of the week for a particular date. They are:

1.	The spin of the Earth on its axis completing one revolution every 24 hrs (sunrise to sunrise). This gives a definition of a solar day.

2.	The orbit of the Earth around the sun completing one orbit every 365.242199… solar days (vernal equinox to vernal equinox). This gives a definition of the mean tropical year (roughly speaking - in case there are any experts reading this).

Measuring time in this manner results in the two measures appearing to act independently of each other, hence the non integral number of days in a tropical year.

Therefore, the main problems to be reconciled are:

1.	There is not an integral number of days in a year
2.	There is not an integral number of weeks in a year

With regard to the first problem, as mentioned above, the solar year is 365.242199… days long (but decreasing all the time) but if the exact figure is not incorporated, the calendar will gradually grow more and more out of synchronisation with the seasons.

For example, if every year were 365 days long and leap years did not exist then after approximately 772 years, Christmas day would fall on the summer solstice.

The introduction of a leap year makes the average year 365.25 days long but if, as in the case of the old Julian calendar (named after Julius Caesar), one only relied on the leap year being inserted once every four years, then after approximately 128 years Christmas day would fall one day late and after 22,818 years Christmas day would fall on the summer solstice.

Pope Gregory XIII was aware of this happening and decided to make a simple change to the calendar viz. a year which is a multiple of 100 is only a leap year if it is also divisible by 400 i.e., 2000, 2400, 2800, etc., are leap years. 2100, 2200, 2300, 2500 etc., are not leap years. This change makes the average Gregorian year 365.2425 days long and is the primary difference with the Gregorian calendar - the calendar used in most of the world today. But even relying on the Gregorian modification, means that after 3,322 years Christmas day will fall exactly one day late and after 591,362 years Christmas day will fall on the summer solstice. The feeling seems to be that the problem can be dealt with after 3,322 years.

Both the Julian and Gregorian calendars are classed as solar calendars because they try to synchronise with the period of the Earth's orbit around the sun. The two other classes of calendars in use in the world today are lunar calendars which try to synchronise with the phases of the moon, and lunisolar calendars which are a combination of the first two.

All the features in the Gregorian calendar have to be taken into account when calculating days outside the twentieth century. 

For now, however, one can just assume the solar year contains 365.25 solar days.

Obviously our current calendar consists of 3 years of 365 days followed by one year of 366 days. This also highlights the non integral number of weeks in a year mentioned above. The non integral number of weeks is not a problem for creating calendars it just makes the arithmetic a little more detailed when calculating the day of the week.

365 days = 52 weeks and 1 day.

366 days = 52 weeks and 2 days.

Therefore if a particular date falls on a Sunday one year, the same date will fall on a Saturday the previous year (52 weeks and 1 day back from a Sunday is a Saturday). Some of this might seem to be stating the obvious but it's probably better to explain too much than too little. Therefore as the 31 Oct 1999 is a Sunday it follows that the 31 Oct 1998 is a Saturday.

So for every year one goes back, the same date will fall one day earlier. Until one reaches a leap year when the same date will fall an extra day earlier. This leads to the following table:

31 Oct 1999	=	Sunday
31 Oct 1998	=	Saturday		0 leap years encountered
31 Oct 1997	=	Friday		0 leap years encountered
31 Oct 1996	=	Thursday	0 leap years encountered
31 Oct 1995	=	Tuesday		1 leap year encountered in Feb 1996
31 Oct 1994	=	Monday		1 leap year encountered in Feb 1996
31 Oct 1993	=	Sunday		1 leap year encountered in Feb 1996
31 Oct 1992	=	Saturday		1 leap year encountered in Feb 1996
31 Oct 1991	=	Thursday	2 leap years encountered in Feb 1996 & Feb 1992
31 Oct 1990	=	Wednesday	2 leap years encountered in Feb 1996 & Feb 1992

Table 1

Therefore to calculate the day on which 31 Oct falls for year 'nn' in the 20th century the algorithm is:

d = 7 - <[(99-nn) + {(99-nn) DIV 4}] MOD 7>

Where:

'd' is a value between 0 and 6 representing the day of the week (0 = Sunday, 6 = Saturday).

'DIV' represents integer division i.e., 6 DIV 4 = 1, 9 DIV 4 = 2.
This is used to calculate the number of leap years traversed.

'MOD' represents modulo arithmetic i.e., 6 MOD 7 = 6, 9 MOD 7 = 2.
This is used to convert days into weeks (i.e. 17 days = 2 weeks and 3 days. 2 weeks on from a Sunday is a Sunday therefore only the 3 days are significant).

Therefore to understand what's happening in the equation above it is useful to choose a particular example.

Using 31 Oct 1990 as an example (nn = 90)

First calculate the number of years between 1999 and 1990: (99-nn) =				   9
Calculate the number of leap years between 1999 and 1990: (99-nn) DIV 4 =			   2
											-----
Add these to give the total number of days by which the two years differ: =			 11

Remove the whole weeks from this figure: 11 MOD 7 =					   4

This shows that 31 Oct 1990 is 4 days further back from 31 Oct 1999

It is preferable to establish how many days further forward the year is, giving: 7 - 4 =		   3

This shows that 31 Oct 1990 is 3 days further forward from 31 Oct 1999. And as 31 Oct 1999 is a Sunday it follows that 31 Oct 1990 is a Wednesday.

Therefore if anyone ever asks you what day the 31 October 19nn is, you'll be able to tell them. However, it's very probable that you'll be asked for dates other than 31 October but do not despair, the algorithm can be extended.

To do this, one must first forget everything that's been stated so far.
Well not everything but at least the bit about 31 Oct 1999.

I don't use that date because it complicates things needlessly. Instead everything is calculated from the day before the 1 Nov 1999. Some would say that's the same as the 31 Oct 1999 but it's preferable to view it as the 0 Nov 1999.

So, 0 Nov 1999 is a Sunday, therefore to work out 5 Nov 1999 one just adds 5 which gives five days further on from a Sunday which is a Friday.

So 5 Nov 1999 is a Friday.

Therefore to calculate 5 Nov 1990 one first uses the value of 3 which has been calculated above for 1990. Adding 5 gives 8 which means 5 Nov 1990 is 8 days further forward from 0 Nov 1999, and 8 days on from a Sunday is a Monday.

The nth Nov 19pq can now be calculated for all n, p, q.

To deal with the month one must first memorise the following table:

		Month		Deviation
				  (days)
Jan		      4†
Feb		      0†
Mar		      0
Apr		      3
May		      5
Jun		      1
Jul		      3
Aug		      6
Sep		      2
Oct		      4
Nov		      0
Dec		      2

Table 2

Table 2 gives the offsets for each month, e.g., for any year a particular date in June will be one day further forward than the same date that year in November, a particular date in August will be six days further forward than the same date that year in November, etc.

Therefore as the algorithm gives a value of 8 for 5 Nov 1990 we find that 5 Sep 1990 will be an additional 2 days further forward giving 10. 10 modulo 7 = 3 which means that 5 Sep 1990 is a Wednesday.

And that's almost all there is to it.

As can be seen in Table 2 above, the maximum number of identical months in any year† is three (February, March and November). It is for this reason that November is chosen as the starting point - it minimises the average amount of mental arithmetic required. Also by choosing 1999 it means that division by 4 gives an accurate representation of the number of leap years encountered. If 2000 had been chosen the algorithm would have shown no leap year being traversed between 0 Nov 1997 and 0 Nov 2000 which of course is wrong. Using the 0th of the month instead of the 1st or the 31st again minimises the amount of mental arithmetic required. Which is why 0 Nov 1999 was chosen. The numbers in Table 2 were chosen so that Monday would equate to the first day of the week, again to make the algorithm easier to use.

One will probably have realised by now that the algorithm still isn't quite right. The problem is that if the desired date falls before March in any leap year then an intercalary day (leap day) will be traversed but the algorithm will not have taken it into account. 

†	This is why all those daggers were included. To overcome this problem one must check to see if the year is a leap year, i.e., divisible by four. If it is a leap year and the month is January or February then it is necessary to subtract one from the final answer to arrive at the correct day. Alternatively Table 2 could be replaced with Table 3 below.

					Deviation
					  (days)
Month		Leap Year	Common Year

Jan		      3		      4
Feb		      6		      0
Mar		      0		      0
Apr		      3		      3
May		      5		      5
Jun		      1		      1
Jul		      3		      3
Aug		      6		      6
Sep		      2		      2
Oct		      4		      4
Nov		      0		      0
Dec		      2		      2

Table 3

A random date: 15 Feb 1940 will now be used as an example to show how the algorithm operates.

To derive the day of the week there are four stages to the calculation, A, B, C and D as described below.

A	Calculate the offset for the year
B	Calculate the offset for the month
C	Calculate the offset for the date
D	Sum all the offsets

Therefore:

A1	1999 - 1940 =		59	(years between 1999 and 1940)
A2	59 DIV 4 =		14	(leap years between 1999 and 1940)
A3	59 + 14 =		73	(single days back from 0 Nov 1999)
A4	73 MOD 7 =		  3	(days back from 0 Nov 1999 excluding whole weeks)
A5	7 - 3 =			 4	(days forward from 0 Nov 1999)

The 4 must now be memorised while the remaining parts are calculated.

B1	Feb =			  0	(usually. See Table 3)
B2	1940 is leap year		 -1	(therefore subtract one. -1 MOD 7 = 6)
B3	Monthly difference =	  6	(or just use 6 straight from Table 3)

The 6 must also be memorised while the final part is considered.

C1	The date =		15

Finally:

D1	4 + 6 + 15 =		 25	(Sum of all the three parts)
D2	25 MOD 7 =		  4	(Four days further on from Sunday 0 Nov 1999)

Therefore 15 Feb 1940 is the fourth day of the week namely Thursday.

Having derived the basic algorithm for the Gregorian calendar for the twentieth century, there are numerous short cuts one can take.

One important short cut is derived from the fact that after 7 leap years have been traversed the offset caused by the introduction of the leap year will be seven days i.e., one whole week. But whole weeks can be ignored i.e., 7 MOD 7 = 0. Therefore every seven leap years (28 years) the calendar for any particular century will repeat itself exactly (leap years and non leap years). This leads to some years in every century which have a special significance viz. 99, 71, 43 and 15. Each of which ends a 28-year cycle. So for example to calculate the date for 1968 it is only necessary to subtract 68 from 71 not from 99.

Also after a while one begins to remember the results derived for Part A, i.e., the year calculation. For example the 1970s are quite easy to remember:

1971 = 0		Because it's the same as 1999 (The 28 year cycle)
1972 = 2		Just the last digit
1973 = 3		Just the last digit
1974 = 4		Just the last digit
1975 = 5		Just the last digit

I also instinctively remember the rest of the 1970s viz.

1976 = 0
1977 = 1
1978 = 2
1979 = 3

By remembering these years and making use of the 28 year cycle other years become easy to calculate e.g.:

1916 = 1915 + 1	= 1971 + 1 =  1972 = 2
1944 = 1943 + 1	= 1971 + 1 =  1972 = 2
1919 = 1915 + 4	= 1971 + 4 =  1975 = 5
1947 = 1943 + 4	= 1971 + 4 =  1975 = 5
etc.

Other patterns in the century can be identified and used to minimise the amount of mental arithmetic required. For example results are contiguous in groups of four years beginning with a leap year (See Appendix 1).

The best advice is to just have a look and spot your own (one of the greatest abilities of the brain is to quickly identify patterns).

To calculate the same date as before: 15 Feb 1940 using short cuts ...

... there are still four components to the calculation: 

A	Calculate the offset for the year
B	Calculate the offset for the month
C	Calculate the offset for the date
D	Sum all the offsets

The algorithm now works as follows:

A1	1943 - 1940 =		3	(years between 1943 and 1940)
A2	3 DIV 4 =		0	(no leap years between 1943 and 1940 - at this stage)
A3	0 + 3 =			3	(single days back from 0 Nov 1943 / 1999)
A4	3 MOD 7 =		3	(days back from 0 Nov 1999 excluding whole weeks)
A5	7 - 3 =			4	(days forward from 0 Nov 1999)

The 4 can easily be memorised as a running total while the other components are calculated.

B1	Feb =			  0	(usually. See Table 3)
B2	1940 is leap year		 -1	(therefore subtract one. -1 MOD 7 = 6)
B3	Monthly difference =	  6	(or just use 6 straight from Table 3)

The 6 can be added to the 4 giving 10 which is 3 in modulo 7 arithmetic.
Or one can just cut out the previous step knowing that 6 modulo 7 = -1 and 4 - 1 = 3.

C1	The date =		15

15 modulo 7 	=		 1
which added to the running total of 3 = 4	(Giving four days further on from Sunday 0 Nov 1999)

Therefore 15 Feb 1940 is the fourth day of the week namely Thursday.

After a while one just sees a set of patterns, cycles, checks and balances so

for:			15 	Feb 	1940

one eventually sees:	1	-1	4
and then:		0	0	4	=	Thursday.

The next stage is to add a fifth part to the algorithm to cater for dates in other centuries within the Gregorian calendar.

As mentioned above, the main feature of the Gregorian calendar is that it is designed to more closely reflect the actual average year length of 365.242199… days. To do this some years which are divisible by 4 do not have an intercalary day (leap day) added. The algorithm used is that if a year is divisible by 100 it is only a leap year if it is also divisible by 400. The effect of this is that there are only 97 leap years in each 400 year period of the Gregorian calendar.

When looking at the pattern of removing 3 leap years every 400 years the following table of deviations are found:

Century		Deviation
   19		      2
   20		      0
   21		      6
   22		      4
   23		      2
   24		      0
   25		      6
   26		      4
   27		      2
   28		      0

It can be seen from the above table that the century component of the algorithm is:

•	subtract the century from the next highest or equal multiple of 4
•	multiply the answer by 2.

For the twentieth century the deviation is zero and can therefore be ignored [(20-20)*4 = 0].

Therefore, to calculate the day for 15 Feb 2140.

The running total for parts A to D is 4.

Continuing...

Century number =						22

Subtract the century number from the next higher multiple of 4 =	2

Multiply by 2 =							4

Add this to the running total 4 + 4 = 8 and 8 modulo 7 = 		1

Therefore, 15 Feb 2140 is the first day of the week namely Monday.

 The Gregorian calendar is thus based on a cycle of 400 years, which comprises 146097 days. Since 146097 is evenly divisible by 7, the Gregorian civil calendar exactly repeats after 400 years. Dividing 146097 by 400 yields an average length of 365.2425 days per calendar year, which is a close approximation to the length of the tropical year.

Although the Gregorian calendar repeats itself every 28 years within a century, the repetition does not extend across all century boundaries. One of the advantages of this is that the calendar which is based on a cycle of 400 years, comprises 146097 days and therefore repeats itself exactly every 400 years and every 10,000 years. Therefore only the last four digits of the year need be considered in any calculation.

Therefore, 15 Feb 347842140 is the same day of the week as 15 Feb 2140, namely Monday. Most people when asking me to calculate a date way in the future usually concentrate on the billions and millions, leaving the last four digits zero, therefore I know right away that …0000 is a leap year with a year value of 2.

For this reason, one can extrapolate forwards using the Gregorian calendar indefinitely without much difficulty, however, as mentioned earlier, after 3,322 years the Gregorian calendar will lose synchronicity with the seasons.

It is also possible to extrapolate backwards with the Gregorian calendar, however, it should be remembered that the calendar was not introduced until 1582 in Catholic Europe, 1752 in Britain and not until the 20th century in some Eastern orthodox countries including Russia and Greece. Extrapolation to times prior to the Gregorian reform is referred to as a proleptic Gregorian calendar.

Before 1582 the calendar most commonly used in the Christian world was the Julian calendar introduced by Julius Caesar in 45 BC.

There’s nothing to stop one extrapolating forwards on the Julian calendar even though it was superseded by the Gregorian calendar ...

The algorithm needs only a small modification to the century calculation in order to handle the Julian calendar viz., subtract the century from the next highest multiple of 7 when added to 4, i.e., 4, 11, 18, 25, …. (See Appendix 3). The main things to remember when calculating dates in the Julian calendar include such things as: the calendar was abandoned at different times in different countries and the first day of the year was 25 Mar i.e., 19 Jan 1427 was what we (and the algorithm) would call 19 Jan 1428 (which was a leap year).

PS I once attempted to create an algorithm to calculate the day on which Easter falls for any given year, but because the algorithm has to accommodate the orbit of the moon around the Earth in addition to the orbit of the Earth around the Sun and the rotation of the Earth about its axis; it became very complicated. Easter falls on the first Sunday following the ecclesiastical Full Moon that falls on or next after March 21. Remembering that March 21 is not always the date of the vernal equinox and the ecclesiastical Full Moon is different to the astronomical Full Moon. The dates of Easter repeat themselves in a cycle of 5,700,000 years.

Anyway, hope you've enjoyed these notes and hurry up and get them memorised.

Gerard

Appendix 1	Yearly offsets

1900	3	1910	1	1920	0	1930	5	1940	4
1901	4	1911	2	1921	1	1931	6	1941	5
1902	5	1912	4	1922	2	1932	1	1942	6
1903	6	1913	5	1923	3	1933	2	1943	0
1904	1	1914	6	1924	5	1934	3	1944	2
1905	2	1915	0	1925	6	1935	4	1945	3
1906	3	1916	2	1926	0	1936	6	1946	4
1907	4	1917	3	1927	1	1937	0	1947	5
1908	6	1918	4	1928	3	1938	1	1948	0
1909	0	1919	5	1929	4	1939	2	1949	1

1950	2	1960	1	1970	6	1980	5	1990	3
1951	3	1961	2	1971	0	1981	6	1991	4
1952	5	1962	3	1972	2	1982	0	1992	6
1953	6	1963	4	1973	3	1983	1	1993	0
1954	0	1964	6	1974	4	1984	3	1994	1
1955	1	1965	0	1975	5	1985	4	1995	2
1956	3	1966	1	1976	0	1986	5	1996	4
1957	4	1967	2	1977	1	1987	6	1997	5
1958	5	1968	4	1978	2	1988	1	1998	6
1959	6	1969	5	1979	3	1989	2	1999	0

Appendix 2	Monthly offsets

					Offset
					(days)
Month		Leap Year	Non Leap Year

Jan		      3		      4
Feb		      6		      0
Mar		      0		      0
Apr		      3		      3
May		      5		      5
Jun		      1		      1
Jul		      3		      3
Aug		      6		      6
Sep		      2		      2
Oct		      4		      4
Nov		      0		      0
Dec		      2		      2

Appendix 3	Century offsets

Gregorian         Julian
Century	Deviation       Deviation
   19		      2		0
   20		      0		6
   21		      6		5
   22		      4		4
   23		      2		3
   24		      0		2
   25		      6		1
   26		      4		0
   27		      2		6
   28		      0		5