# [Life and Math #16] Finding the Date of the week - Zeller's Formula

in math •  10 months ago  (edited)

[1]

## Finding the Day of the Week - Zeller's Formula

Today's topic is not complicated, rather it is closely related to our daily lives.

## 0. What is Zeller's Formula?

[2]
Zeller's congruence is an algorithm devised by Christian Zeller to calculate the day of the week for any Gregorian calendar date. The weird formula is given as follows.

$f&space;=&space;k&space;+&space;\lfloor&space;\frac{13m&space;-&space;1}{5}&space;\rfloor&space;+&space;d&space;+&space;\lfloor&space;\frac{d}{4}&space;\rfloor&space;+&space;\lfloor&space;\frac{c}{4}&space;\rfloor&space;-&space;2c&space;\mod&space;7$

where

• $k$ is the day of the month

• $m$ is the month number, where March = 1, April = 2, ..., December = 10, January = 11, February = 12

• $d$ is the last two digits of year, using the previous year for January and February

• $c$ is the first two digits of year

• mod 7 represents the remainder after dividing $f$ by 7, to find the day of the week where Sunday = 0, Monday = 1, ..., Saturday = 6.

Let's see some examples. March 10, 2014 corresponds to

$k=&space;10,\&space;m&space;=&space;1,\&space;d&space;=&space;14,\&space;c&space;=&space;20$

and this leads to $f&space;=&space;-6&space;\implies&space;f&space;\equiv&space;1&space;\mod&space;7$ so that the date was Monday. Another example, February 10, 2010 corresponds to

$k&space;=&space;10,\&space;m=12,\&space;d&space;=&space;9,\&space;c&space;=&space;20$

because $d$ comes from the previous year for February (which is 2009). This leads to $f&space;=&space;17&space;\implies&space;f&space;\equiv&space;3&space;\mod&space;7$, so that the date was Wednesday. Now simple questions arise naturally,

Why does this work?

How do you derive the formula for the day of the week?

Let's find out.

### 1. Weird counting on the month number.

Let's first note the reason for the odd handling of months. Why not starting from January? This is because we want leap day not to affect the formula, so we move it to the end of the 'year', and act as if the year began on March 1. (You know Feb 29...)

### 2. Strategy Behind the summations

Note that in defining $f$, all we care about is the remainder after dividing, so it will be enough to make sure that $f$ increases by 1 whenever the day of the week advances by one day; we don't care about the actual value of $f$. So it is enough to add or subtract all the factors effecting the day of the week in order to make the formula valid.

### 3. Affect of Year

Normally, 1 year is equivalent to 365 days. Since $365&space;=&space;7&space;\times&space;52&space;+&space;1$ , each normal year advances the day of the week by 1, so nothing more to manipulate! Just put the year number in the formula.

$f&space;=&space;d$

### 4. Affect of Leap year

But we have to adjust this to account for leap years. Every four years we have an extra day (a year that is divisible by four), so we'll want to add 1 to f. This is done by adding $\lfloor&space;d/4&space;\rfloor$ , since this increases by 1 only when d becomes a multiple of 4, which is a leap year. So now we have

$f&space;=&space;d&space;+&space;\lfloor&space;d/4&space;\rfloor$

### 5. Affect of Years ending with 00

But according to Gregorian Calendar rule, not every year that is exactly divisible by four is a leap year. We must exclude years that are exactly divisible by 100 but not with 400. That is, we must exclude years ending with 00 but not ones that are divisible by 400.

Every century contains 100 years, 24 of which normally are leap years (excluding ones with 00 end). So typically, each century the day advances by

$124&space;=&space;7&space;\times&space;17&space;+&space;5&space;=&space;7&space;\times&space;18&space;-&space;2$

days. Therefore the day of the week goes back 2 days. Each century can be represented as the first two digit of year (17th century is the collection of years starting with two digits 16, 20th century is the collection of years starting with two digits 19). So we must modify our formula to

$f&space;=&space;d&space;+&space;\lfloor&space;d/4&space;\rfloor&space;-&space;2c$

where $c$ is the first two digits of the year.

Also the years ending with 00 but not divisible by 400 are the ones that $c$ is divisible by 4, so we must add the term

$f&space;=&space;d&space;+&space;\lfloor&space;d/4&space;\rfloor&space;-&space;2c&space;+&space;\lfloor&space;c/4&space;\rfloor$

as we did for leap years (same argument actually).

### 6. The Hardest Part

[3]
Now we must consider months and days. Consider, for each month, how many days it has beyond 28, then add that up to see the effect the months have on the day of the week. The reason of choice of 28 as the reference is that it is divisble by 7 (not affecting the day of the week).

m12345
MarAprMayJunJul
# of days3130313031
Excess32323
Accumulated Days035810
6789101112
AugSepOctNovDecJanFeb
31303130313128
3232330
13161821232629

Here, we don't have to worry about leap years because we already considered them on previous sections. The number of accumulated days is counted at the start of the month, so if we divide it by 7, the remainder shows how many weekdays the start of the month is from the starting day for the 'year'. Notice the pattern in the excess: 3,2,3,2,3 repeats every five months, and the accumulation reaches 13 in that time. So every 5 months, we want to add 13 days. This can be modeled by the equation of the form

$\lfloor&space;\frac{13m}{5}&space;+&space;\alpha&space;\rfloor$

where $\alpha$ is the constant that should be determined from specific values in accumulated days. Let's solve this.

• Put $m&space;=&space;1$ :

$\lfloor&space;\frac{13}{5}&space;+&space;\alpha&space;\rfloor&space;=&space;0&space;\implies&space;-13/5&space;\leq&space;\alpha&space;<&space;-8/5$

• Put $m&space;=&space;2$:

$\lfloor&space;\frac{26}{5}&space;+&space;\alpha&space;\rfloor&space;=&space;3&space;\implies&space;-11/5&space;\leq&space;\alpha&space;<&space;-6/5$

• Put $m&space;=&space;3$:

$\lfloor&space;\frac{39}{5}&space;+&space;\alpha&space;\rfloor&space;=&space;5&space;\implies&space;-14/5&space;\leq&space;\alpha&space;<&space;-9/5$

• Put $m&space;=&space;4$:

$\lfloor&space;\frac{52}{5}&space;+&space;\alpha&space;\rfloor&space;=&space;8&space;\implies&space;-12/5&space;\leq&space;\alpha&space;<&space;-7/5$

• Put $m&space;=&space;5$:

$\lfloor&space;\frac{65}{5}&space;+&space;\alpha&space;\rfloor&space;=&space;10&space;\implies&space;-3&space;\leq&space;\alpha&space;<&space;-10/5$

Beyond 5 is just repetition, so those are rebundant information. Now,

$-11/5&space;\leq&space;\alpha&space;<&space;-10/5&space;=&space;2$

Any number in the half open interval $[-11/5,&space;2)$ is valid. For convenience, let's take $\alpha&space;=&space;-11/5$ , so that

$\lfloor&space;\frac{13m}{5}&space;+&space;\alpha&space;\rfloor&space;=&space;\lfloor&space;\frac{13(m-1)}{5}&space;\rfloor&space;-&space;2$

(The reason behind this choice is stated in Section 7). Now the modified equation for $f$ is

$f&space;=&space;d&space;+&space;\lfloor&space;d/4&space;\rfloor&space;-&space;2c&space;+&space;\lfloor&space;c/4&space;\rfloor&space;+&space;\lfloor&space;\frac{13(m-1)}{5}&space;\rfloor&space;-2$

Finally, we have to add the day, since each day obviously adds one to the day of the week. Thus

$f&space;=&space;d&space;+&space;\lfloor&space;d/4&space;\rfloor&space;-&space;2c&space;+&space;\lfloor&space;c/4&space;\rfloor&space;+&space;\lfloor&space;\frac{13(m-1)}{5}&space;\rfloor&space;-2&space;+&space;k$

Are we done? Well, no. Nothing we've done so far actually determined which day we start the whole pattern on. Any initial condition should be given. I'll choose today, August 30, 2018. Today is Thursday so that $f&space;\equiv&space;4&space;\mod&space;7$ . Calculation gives

$f&space;=&space;30&space;\implies&space;f&space;\equiv&space;2&space;\mod&space;7$

Now the adjustment, is nothing but adding 2! (This is the reason why we chose -11/5 as $\alpha$). Therefore the ultimate equation for the day of the week is

$f&space;=&space;k&space;+&space;\lfloor&space;\frac{13m&space;-&space;1}{5}&space;\rfloor&space;+&space;d&space;+&space;\lfloor&space;\frac{d}{4}&space;\rfloor&space;+&space;\lfloor&space;\frac{c}{4}&space;\rfloor&space;-&space;2c&space;\mod&space;7$

## 8. Application?

As you can see, the equation is valid as long as Gregorian Calendar rule does not change. Great!

## 9. Conclusion

Formula looks dirty but idea is simple... Just follow the Gregorian Caldener rule! Straightforward enough.

## 10. Citations

Lastly I will post my implementation of Zeller's formula using Python.

def Zeller(D, M, Y):
# Preprocess Year
if Y < 1582:
return 'Before Gregorian'
# Define m
if M < 3:
m = M + 10
else:
m = M - 2
# Define k
k = D
# Define d
if m > 10:
d = int(str(Y - 1)[-2:])
else:
d = int(str(Y)[-2:])
# Define c
c = int(str(Y)[: 2])
print (k, m, d, c)
L = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
f = (k + (13*m-1)//5 + d + (d//4) + (c//4) - 2 * c) % 7
return 'The date of the week is '+ L[f]
#--------------------------
D, M, Y = map(int, input('Enter specific date in DD/MM/YYYY form: ').split('/'))
print (Zeller(D,M,Y))

Sort Order:

You have a minor misspelling in the following sentence:

As you can see, the equation is valid as long as Gregorian Calender rule does not change.
It should be calendar instead of calender.

·

Thanks! I fixed it.

Formula looks dirty but idea is simple...

This quote sums up ur whole article bro. :D Formulas are always confusing but the whole thing looks useful!
Good write, man!

·

Thanks! I'll try to post quality materials on Steemit.

This post has been voted on by the steemstem curation team and voting trail.

There is more to SteemSTEM than just writing posts, check here for some more tips on being a community member. You can also join our discord here to get to know the rest of the community!