EFE_datetime

By Wolfgang Keller
Originally written 2020-10-19
Last modified 2020-10-19

Table of contents

Notations

Let \(x\,\%\,n\) (\(x \in \mathbb{Z}\), \(n \in \mathbb{Z}_{\geq 1}\)) denote the remainder in \(\{0, \ldots, n\}\) of the division of \(x\) by \(n\).

Day of week of 1st of January of a given year

We want to find a formula for the weekday of the 1st of January in a given year \(A\) (\(A\): annus; Latin for “year”).

Gauß' formula

On Determination of the day of the week - Wikipedia [visited 2020-10-19T20:13:02Z], one can find that Carl Friedrich Gauß described the following method for this:

Let the weekdays be numbers by \(0\): Sunday, …, \(6\): Saturday. Then the day of week of 1st of January of a given year \(A\), which we want to denote by \(d(A)\), is \begin{equation} d(A) = (1 + 5 \cdot ((A-1) \,\%\, 4) + 4 \cdot ((A-1) \,\%\, 100) + 6 \cdot ((A-1) \,\%\, 400) ) \,\%\, 7. \label{eq:1stofJan_1} \end{equation}

Where does \eqref{eq:1stofJan_1} come from?

Simplified assumption 1: every year has 365 days

Let us start with the (wrong) assumption that every year had 365 days. Then we would have \[d(A) = (C_1 + 365 \cdot A) \,\%\, 7 = (C_1 + A) \,\%\, 7 = (C'_1 + (A-1)) \,\%\, 7\] for suitable constants \(C_1, C'_1 \in \{0, \ldots, 6\}\).

Simplified assumption 2: Julian calendar

Let us somewhat correct this assumption: every 4th year is a leap year (Julian calendar). Then we must add \(1\) to the inner sum for every leap year that occured before year \(A\). Thus, we obtain the following formula \[d(A) = \left(C_2 + (A-1) + \left\lfloor \frac{A-1}{4} \right\rfloor\right) \,\%\, 7\] for a suitable constant \(C_2 \in \{0, \ldots, 6\}\).

How can the inner sum of this formula be simplified? Consider that \[ \left\lfloor \frac{A-1}{4} \right\rfloor = \frac{A-1 - ((A-1) \,\%\, 4)}{4}. \] Since \(4 \nmid (A-1 - ((A-1) \,\%\, 4))\), and \(4\) is a unit in the ring \(\mathbb{Z}_7\) (with multiplicative inverse \(2\)), we obtain \begin{align*} \left\lfloor \frac{A-1}{4} \right\rfloor \,\%\, 7 & = (2 \cdot (A-1 - ((A-1) \,\%\, 4)))\,\%\, 7 \\ & = (2 \cdot (A-1) + 2 \cdot 6 \cdot ((A-1) \,\%\, 4)) \,\%\, 7 \\ & = (2 \cdot (A-1) + 5 \cdot ((A-1) \,\%\, 4)) \,\%\, 7. \end{align*} Thus, \begin{equation} d(A) = (C_2 + 3 \cdot (A-1) + 5 \cdot ((A-1) \,\%\, 4)) \,\%\, 7.\label{eq:1stofJan_2} \end{equation} Indeed, this formula (with \(C_2 = 6\)) is also written down in the section of the above-linked Wikipedia article.

Gregorian calendar

Now, we have prepared everything to derive \eqref{eq:1stofJan_1}.

Consider that in the Gregorian calender, a year \(A\) is not a leap year if \(A \,\%\, 100 = 0\), except if \(A \,\%\, 400 = 0\). This implies the following modification of \eqref{eq:1stofJan_2}: \begin{equation} d(A) = \left(C_3 + 3 \cdot (A-1) + 5 \cdot ((A-1) \,\%\, 4) - \left\lfloor \frac{A-1}{100} \right\rfloor + \left\lfloor \frac{A-1}{400} \right\rfloor\right) \,\%\, 7. \label{eq:1stofJan_3} \end{equation} for some constant \(C_3 \in \{0, \ldots, 6\}\).

With the same argument that we saw in section , we obtain: \begin{align*} \left\lfloor \frac{A-1}{100} \right\rfloor \,\%\, 7 & = (4 \cdot (A-1 - ((A-1) \,\%\, 100)))\,\%\, 7 \\ & = (4 \cdot (A-1) + 4 \cdot 6 \cdot ((A-1) \,\%\, 100)) \,\%\, 7 \\ & = (4 \cdot (A-1) + 3 \cdot ((A-1) \,\%\, 100)) \,\%\, 7, \end{align*} and \begin{align*} \left\lfloor \frac{A-1}{400} \right\rfloor \,\%\, 7 & = (1 \cdot (A-1 - ((A-1) \,\%\, 400)))\,\%\, 7 \\ & = ((A-1) + 6 \cdot ((A-1) \,\%\, 400)) \,\%\, 7. \end{align*} Using this, \eqref{eq:1stofJan_3} simplifies to \begin{align*} d(A) & = (C_3 + 3 \cdot (A-1) + 5 \cdot ((A-1) \,\%\, 4) - (4 \cdot (A-1) + 3 \cdot ((A-1) \,\%\, 100)) + ((A-1) + 6 \cdot ((A-1) \,\%\, 400))) \,\%\, 7 \\ & = (C_3 + 5 \cdot ((A-1) \,\%\, 4) + 4 \cdot ((A-1) \,\%\, 100)) + 6 \cdot ((A-1) \,\%\, 400)) \,\%\, 7. \end{align*}

The only component missing is the correct value for the constant \(C_3\). The probably simplest way to obtain it is to insert some year \(A\) into the derived formula. This way, one obtains, using the day numbering from section , that \[C_3 = 1.\] Thus, we obtain the final formula \eqref{eq:1stofJan_1}: \[d(A) = (1 + 5 \cdot ((A-1) \,\%\, 4) + 4 \cdot ((A-1) \,\%\, 100)) + 6 \cdot ((A-1) \,\%\, 400)) \,\%\, 7.\]