$$\def\|{&}\DeclareMathOperator{\D}{\bigtriangleup\!} \DeclareMathOperator{\d}{\text{d}\!}$$

$$\DeclareMathOperator{\trunc}{trunc} \DeclareMathOperator{\Div}{div} \DeclareMathOperator{\gcd}{gcd} \DeclareMathOperator{\bdom}{dom} \def\floorratio#1#2{\left\lfloor \dfrac{#1}{#2} \right\rfloor} \def\ceilratio#1#2{\left\lceil \dfrac{#1}{#2} \right\rceil} \def\mod1ratio#1#2{\left\lfloor \dfrac{#1}{#2} \right\rceil_1} \def\dom1ratio#1#2{\left\lceil \dfrac{#1}{#2} \right\rfloor_1}$$

In astronomical formulas that contain a date, it is not convenient to write that date as a combination of years, months, and days, especially because not all years have the same number of days and not all months have the same number of days. It is much more convenient to measure the date as the number of days since some fixed day. The Julian Day Number (JDN) or Julian Date (JD) and its relatives are much used for this in astronomy. This page explains how you can translate a date from various calendars to the Julian Date, or the other way around.

The first part of this page provides algorithms for some modern and historical calendars. The second part (from Section 12) explains how I deduced those algorithms.

There are many different algorithms in use for converting between calendar dates and day numbers. The only thing that counts is that they provide the correct answers for all day numbers and calendar dates. If there is only a limited number of input values and possible outcomes (for example for calculating the month number and day-within-the-month number from the day-within-the-year number), then you can usually find many different algorithms that all give just the right answers for those particular values (but perhaps very different answers for all other input values), even if there is no logical connection between that algorithm and the calendar. In such a case there is often also little or no logical connection between the algorithm that converts from date to day number and the algorithm that converts from day number to date, but the algorithms may be shorter then.

For every algorithm, you should know for which input values it was designed to work. Some calendar algorithms only work well with positive numbers and give wrong results for negative years or negative day numbers. The algorithms I give below are designed to work for all years (including negative ones), all months in each year, all days in each month, and for all Julian Day Numbers (including negative ones).

Some of the fomulas express a choice: If a certain condition is met, then a certain action should be taken (for example, apply a correction to something), and if that condition is not met, then a different action or no action at all should be taken. Such alternative paths are easy to use in manual calculations, in applications that process one date at a time, and in applications written in a programming language that requires compilation before the program can be executed (for example, written in C or Fortran) but are not very convenient for use in applications that can handle many dates at once (in an array) and that are written in a programming language that can be executed immediately (such as Basic or Perl or IDL), for which the execution of an if-then statement per date is much slower than the execution of a single fixed calculation for each date in an array.

If the calendrical calculations below here involve such choices, then where possible I give a formula that circumvents if-then constructions and is therefore convenient for application to arrays of dates in direct-execution programming languagues. The accompanying text explains which choice is being worked around.

## 1. Different Kinds of Day Numbers

For astronomical calculations that depend on time it is useful to have a continuous time scale that uses only a single unit, and not three like most calendars (days, months, years), and that in addition uses a fixed time zone so that there is no confusion about which precise instant of time is meant.

The IAU has adopted the Julian Date (JD) for this purpose. This should not be confused with "a date in the Julian Calendar". There are various other timescales that look like JD. The next table describes a few of them.

Name Kind Begins From JD
JD Julian Date Fractional 12:00 UTC
JDN Julian Day Number Whole 12:00 UTC = ⌊JD⌋
CJD Chronological Julian Date Fractional 00:00 LT = JD + 0.5 + TZ
CJDN Chronological Julian Day Number Whole 00:00 LT = ⌊JD + 0.5 + TZ⌋

"Whole" means that that timescale uses whole numbers only and counts whole days only. "Fractional" means that that timescale uses fractional numbers (with a part after the decimal point) and indicates instants of time. The "Begins" column shows when each next calendar day begins. 12:00 UTC means that a new calendar day begins at 12:00 (noon) universal time, which is not 12:00 noon local time ― except if you happen to be in a time zone equal to UTC, such as in Great Britain in winter. 00:00 LT means that a new calendar day begins at midnight local time. The "From JD"-column shows how the other numbers can be calculated from JD. In that column, "TZ" stands for an adjustment for your local time zone; that adjustment is 0 for UTC.

In practice, "fractional" JDs and CJDs are usually written with at least one digit after the decimal marker (even if that digit is 0), and "whole" JDNs and CJDNs without digits after the decimal marker.

The zero point of JD (i.e., JD 0.0) corresponds to 12:00 UTC on 1 January −4712 in the Julian calendar. The zero point of CJD corresponds to 00:00 (midnight) local time on 1 January −4712. JDN 0 corresponds to the period from 12:00 UTC on 1 January −4712 to 12:00 UTC on 2 January −4712. CJDN 0 corresponds to 1 January −4712 (the whole day, in local time).

Let's see which instants or periods of time correspond in the Gregorian calendar to the number 2455772 in the timescales of the previous table, in a time zone in which the clock reads 2 hours later than UTC (for example, in Central Europe in summer).

 JD 2.45577e+06 exactly at 14:00 hours on 29 July 2011 JDN 2.45577e+06 from 14:00 hours on 29 July 2011 until 14:00 hours on 30 July 2011 CJD 2.45577e+06 exactly at 00:00 hours (midnight) on 29 July 2011 CJDN 2.45577e+06 the whole calendar day of 29 July 2011

To avoid round-off errors it is best to do calendrical calculations with whole numbers only. In what follows we use CJDN for that.

## 2. Notation

### 2.1. Rounding

In the formulas given below, many numbers are rounded to a nearby whole number. It is important that those numbers get rounded in the right direction. Whole numbers are already rounded, so those don't change.

The types of rounding that we use here are downward rounding to the nearest whole number (for $$x$$ this is indicated by $$⌊x⌋$$) or upward rounding to the nearest whole number ($$⌈x⌉$$). The standard rounding function on calculators usually rounds to the nearest whole number (our notation for that is $$[x]$$) and the standard conversion from a fractional number to a whole number in a computer language is usually by rounding in the direction of zero (our notation for that is $$\trunc(x)$$). We do not want those kinds of rounding here! The below table shows these three different kinds of rounding for some values.

 $${x}$$ $${⌊x⌋}$$ $${[x]}$$ $${⌈x⌉}$$ $${\trunc(x)}$$ −5 −5 −5 −5 −5 −4.9 −5 −5 −4 −4 −4.2 −5 −4 −4 −4 −4 −4 −4 −4 −4 −0.2 −1 0 0 0 0 0 0 0 0 0.2 0 0 1 0 4 4 4 4 4 4.2 4 4 5 4 4.9 4 5 5 4 5 5 5 5 5

### 2.2. Modular Arithmetic

If you divide the whole number $$y$$ by the whole number $$x$$ then you get a quotient $$q$$ and a remainder $$r$$. Sometimes we're interested in the quotient, and sometimes in the remainder. We choose to have the remainder never be negative. Then $$0 ≤ r \lt |x|$$ and

\begin{align} q \| = \floorratio{y}{x} \\ r \| = y \bmod x = y − x\floorratio{y}{x} \\ y \| = qx + r \end{align}

The notation $$y \bmod x$$ means: the non-negative remainder from the division of $$y$$ by $$x$$.

We find

\begin{equation} \frac{y}{x} = \floorratio{y}{x} + \left( \frac{y}{x} \bmod 1 \right) = \floorratio{y}{x} + \frac{y \bmod x}{x} \end{equation}

and if $$x = 1$$ then

\begin{equation} y = ⌊y⌋ + (y \bmod 1) \end{equation}

The notation $$x ≡ y \pmod{n}$$ means that $$x$$ and $$y$$ differ by some multiple of $$n$$. One says that $$x$$ and $$y$$ are congruent modulo $$n$$, and $$n$$ is called the modulus. The equation $$x ≡ y \pmod{n}$$ is called a congruence.

Modular arithmetic is arithmetic in which all calculations are modulo a fixed $$n$$. That means that you can subtract arbitrary multiples of $$n$$ from all terms and factors without changing the congruence. If $$x ≡ y \pmod{n}$$ and $$p$$ is a whole number, then also $$x + pn ≡ y \pmod{n}$$, and $$px ≡ py \pmod{n}$$, and also $$px ≡ py \pmod{pn}$$.

A well-known example of modular arithmetic is clock face arithmetic. The face of a typical clock can show hours between 1 and 12. If you see two pictures of a clock face and in the first picture it shows 3 o'clock and in the second picture it shows 7 o'clock, then the second picture might have been made 4 hours after the first one, but also 4 + 12 = 16 hours later, or 4 + 10×12 hours = 5 days and 4 hours later, or 4 hours plus any desired multiple of 12 hours later (or earlier). If $$t_1$$ is the time of the first picture and $$t_2$$ is the time of the second picture (measured in hours from a fixed moment), then all you know from the pictures is that $$t_2 ≡ t_1 + 4 \pmod{12}$$. And if the clock shows $$t$$ hours now, then after $$y$$ more hours it will show $$(t + y) \bmod 12$$ hours.

Note the difference in notation: $$z = x \bmod y$$ means that $$z$$ is equal to the remainder of dividing $$x$$ by $$y$$, and $$z ≡ x \pmod{y}$$ means that $$z$$ is equal to $$x$$ except for an arbitrary multiple of $$y$$. If $$z = x \bmod y$$ then also $$z ≡ x \pmod{y}$$, but the opposite need not be true,

## 3. The GregorianCalendar

See section 13.2.1 for the derivation of this algorithm.

### 3.1. From Gregorian Date to CJDN

One algorithm to calculate a CJDN $$J$$ from a Gregorian date (calendar year $$j$$, calendar month $$m$$, calendar day $$d$$) is:

\begin{align} c_0 \| = \floorratio{m − 3}{12} \\ x_4 \| = j + c_0 \\ \{x_3, x_2\} \| = \Div(x_4, 100) \\ x_1 \| = m − 12c_0 − 3 \\ J \| = \floorratio{146097x_3}{4} + \floorratio{36525x_2}{100} + \floorratio{153x_1 + 2}{5} + d + 1721119 \end{align}

Here $$J_1$$ is the number of days from the beginning of calculation year 0 until the beginning of the current 100-calculation-year-period, $$J_2$$ the number of days since the beginning of the current 100-calculation-year-period, and $$J_3$$ the number of days minus one from the beginning of the current calculation year until the beginning of the current month. Calculation years run from 1 March to 1 March.

For example: what is the CJDN for 7 September 2010 on the Gregorian calendar? Then $$j = 2010$$, $$m = 9$$, $$d = 7$$ so

\begin{align*} c_0 \| = \floorratio{9 − 3}{12} = 0 \\ x_4 \| = 2010 + 0 = 2010 \\ \{x_3, x_2\} \| = \Div(2010, 100) = \{20, 10\} \\ x_1 \| = 9 − 12×0 − 3 = 6 \\ J \| = \floorratio{146097×20}{4} + \floorratio{36525×10}{100} + \floorratio{153×6 + 2}{5} + 7 + 1721119 \\ \| = 730485 + 3652 + 184 + 7 + 1721119 = 2455447 \end{align*}

And here are the calculations for a few more dates:

calendar date $${c_0}$$ $${x_4}$$ $${x_3}$$ $${x_2}$$ $${x_1}$$ $${…x_3…}$$ $${…x_2…}$$ $${…x_1…}$$ $${J}$$
2000-02-29 −1 1999 19 99 11 693960 36159 337 2451604
2000-03-01 0 2000 20 0 0 730485 0 0 2451605
2001-02-28 −1 2000 20 0 11 730485 0 337 2451969
2001-03-01 0 2001 20 1 0 730485 365 0 2451970
2100-02-28 −1 2099 20 99 11 730485 36159 337 2488128
2100-03-01 0 2100 21 0 0 767009 0 0 2488129

### 3.2. From CJDN to Gregorian Date

The algorithm to calculate a Gregorian date (calendar year $$j$$, calendar month $$m$$, calendar day $$d$$) from a CJDN $$J$$ is:

\begin{align} \{x_3, r_3\} \| = \Div(4×J − 6884477, 146097) \\ \{x_2, r_2\} \| = \Div\left( 100\floorratio{r_3}{4} + 99, 36525 \right) \\ \{x_1, r_1\} \| = \Div\left( 5\floorratio{r_2}{100} + 2, 153 \right) \\ d \| = \floorratio{r_1}{5} + 1 \\ c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = 100x_3 + x_2 + c_0 \\ m \| = x_1 − 12c_0 + 3 \end{align}

Here $$x_3$$ is the number of calculation centuries since that time, $$x_2$$ the number of calculation years since that time, and $$x_1$$ the number of months since the beginning of the current calculation year.

For example, which date in the Gregorian calendar corresponds to CJDN 2452827? Then $$J = 2452827$$ and then

\begin{align*} k_3 \| = 4×(2452827 − 1721120) + 3 = 2926831 \\ x_3 \| = \floorratio{2926831}{146097} = 20 \\ k_2 \| = 100\floorratio{2926831 \bmod 146097}{4} + 99 = 100\floorratio{4891}{4} + 99 = 100×1222 + 99 = 122299 \\ x_2 \| = \floorratio{122299}{36525} = 3 \\ k_1 \| = 5\floorratio{122299 \bmod 36525}{100} + 2 = 5×\floorratio{12724}{100} + 2 = 5×127 + 2 = 637 \\ x_1 \| = \floorratio{k_1}{153} = \floorratio{637}{153} = 4 \\ c_0 \| = \floorratio{4 + 2}{12} = 0 \\ j \| = 100×20 + 3 + 0 = 2003 \\ m \| = 4 − 12×0 + 3 = 7 \\ d \| = \floorratio{637 \bmod 153}{5} + 1 = \floorratio{25}{5} + 1 = 5 + 1 = 6 \end{align*}

The date is 6 July 2003.

And here are the calculations for the same dates as before.

$${J}$$ $${x_3}$$ $${x_2}$$ $${x_1}$$ $${c_0}$$ $${j}$$ $${m}$$ $${d}$$ calendar date
2451604 19 99 11 1 2000 2 29 2000-02-29
2451605 20 0 0 0 2000 3 1 2000-03-01
2451969 20 0 11 1 2001 2 28 2001-02-28
2451970 20 1 0 0 2001 3 1 2001-03-01
2488128 20 99 11 1 2100 2 28 2100-02-28
2488129 21 0 0 0 2100 3 1 2100-03-01

## 4. The Milanković Calendar

See section 13.3 for the derivation of this algorithm.

### 4.1. From Milanković Date to CJDN

Some Eastern Orthodox churches use a calendar invented by Milutin Milanković, that differs from the Gregorian calendar only in the rules for which century years are leap years. One algorithm to calculate a CJDN $$J$$ from a Milanković date (calendar year $$j$$, calendar month $$m$$, calendar day $$d$$) is:

\begin{align} c_0 \| = \floorratio{m − 3}{12} \\ x_4 \| = j + c_0 \\ x_3 \| = \floorratio{x_4}{100} \\ x_2 \| = x_4 \bmod 100 \\ x_1 \| = m − 12c_0 − 3 \\ J \| = \floorratio{328718x_3 + 6}{9} + \floorratio{36525x_2}{100} + \floorratio{153x_1 + 2}{5} + d + 1721119 \end{align}

### 4.2. From CJDN to Milanković Date

An algorithm to convert CJDN $$J$$ to year $$j$$, month $$m$$, day $$d$$ in the Milanković calendar is:

\begin{align} k_3 \| = 9×(J − 1721120) + 2 \\ x_3 \| = \floorratio{k_3}{328718} \\ k_2 \| = 100\floorratio{k_3 \bmod 328718}{9} + 99 \\ x_2 \| = \floorratio{k_2}{36525} \\ x_1 \| = \floorratio{5\floorratio{k_2 \bmod 36525}{100} + 2}{153} \\ c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = 100x_3 + x_2 + c_0 \\ m \| = x_1 − 12c_0 + 3 \\ d \| = \floorratio{k_1 \bmod 153}{5} + 1 \end{align}

## 5. The Julian Calendar

See section 13.1 for the derivation of this algorithm.

### 5.1. From Julian Date to CJDN

The algorithm to calculate a CJDN $$J$$ from a Julian date (calendar year $$j$$, calendar month $$m$$, calendar day $$d$$) is:

\begin{align} J_0 \| = 1721117 \\ c_0 \| = \floorratio{m − 3}{12} \\ J_1 \| = \floorratio{1461×(j + c_0)}{4} \\ J_2 \| = \floorratio{153m − 1836c_0 − 457}{5} \\ J \| = J_1 + J_2 + d + J_0 \end{align}

Here $$J_1$$ is the number of days between the beginning of calculation year 0 and the beginning of the current calculation year, and $$J_2$$ the number of days between the beginning of the current calculation year and the beginning of the current month.

For example: what is the CJDN for 7 September 2010 on the Julian calendar? Then $$j = 2010$$, $$m = 9$$, $$d = 7$$

\begin{align*} c_0 \| = \floorratio{9 − 3}{12} = 0 \\ J_1 \| = \floorratio{1461×(2010 + 0)}{4} = 734152 \\ J_2 \| = \floorratio{153×9 − 1836×0 − 457}{5} = 184 \\ J \| = 734152 + 184 + 7 + 1721117 = 2455460 \end{align*}

And here are the calculations for a few more dates:

calendar date $${c_0}$$ $${J_1}$$ $${J_2}$$ $${J}$$
2000-02-29 −1 730134 337 2451617
2000-03-01 0 730500 0 2451618
2001-02-28 −1 730500 337 2451982
2001-03-01 0 730865 0 2451983
2100-02-28 −1 766659 337 2488141
2100-02-29 −1 766659 337 2488142
2100-03-01 0 767025 0 2488143

### 5.2. From CJDN to Julian Date

The algorithm to calculate a Julian date (calendar year $$j$$, calendar month $$m$$, calendar day $$d$$) from a CJDN $$J$$ is:

\begin{align} y_2 \| = J − 1721118 \\ k_2 \| = 4y_2 + 3 \\ k_1 \| = 5\floorratio{k_2 \bmod 1461}{4} + 2 \\ x_1 \| = \floorratio{k_1}{153} \\ c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = \floorratio{k_2}{1461} + c_0 \\ m \| = x_1 − 12c_0 + 3 \\ d \| = \floorratio{k_1 \bmod 153}{5} + 1 \end{align}

Which Julian date corresponds to CJDN 2451893? Then $$J = 2451893$$ so

\begin{align*} y_2 \| = 2451893 − 1721118 = 730775 \\ k_2 \| = 4×730775 + 3 = 2923103 \\ k_1 \| = 5\floorratio{2923103 \bmod 1461}{4} + 2 = 5\floorratio{1103}{4} + 2 = 5×275 + 2 = 1377 \\ x_1 \| = \floorratio{1377}{153} = 9 \\ c_0 \| = \floorratio{9 + 2}{12} = \floorratio{11}{12} = 0 \\ j \| = \floorratio{2923103}{1461} + 0 = 2000 + 0 = 2000 \\ m \| = 9 − 12×0 + 3 = 12 \\ d \| = \floorratio{1377 \bmod 153}{5} + 1 = \floorratio{0}{5} + 1 = 1 \end{align*}

The date is 1 December 2000.

And here are the calculations for the same dates as before.

$${J}$$ $${y_2}$$ $${k_2}$$ $${k_1}$$ $${x_1}$$ $${c_0}$$ $${j}$$ $${m}$$ $${d}$$ calendar date
2451617 730499 2921999 1827 11 1 2000 2 29 2000-02-29
2451618 730500 2922003 2 0 0 2000 3 1 2000-03-01
2451982 730864 2923459 1822 11 1 2001 2 28 2001-02-28
2451983 730865 2923463 2 0 0 2001 3 1 2001-03-01
2488141 767023 3068095 1822 11 1 2100 2 28 2100-02-28
2488142 767024 3068099 1827 11 1 2100 2 29 2100-02-29
2488143 767025 3068103 2 0 0 2100 3 1 2100-03-01

## 6. The Islamic Calendar

See section 13.8 for the derivation of this algorithm.

### 6.1. From Islamic Date to CJDN

The religious Islamic calendar depends on observations and so cannot be caught in formulas. The administrative calendar has fixed rules so it can be caught in formulas. The difference between the religious and administrative calendars should usually be no more than 1 day.

The CJDN can be derived from the year number $$j$$, month number $$m$$, and day number $$d$$ in the most commonly used administrative Islamic calendar (used by al-Fazārī, al-Khwārizmī, al-Battānī, and in the Toledan and Alfonsine Tables) as follows:

\begin{equation} J = \floorratio{10631j − 10617}{30} + \floorratio{325m − 320}{11} + d + 1948439 \end{equation}

For example, which CJDN $$J$$ corresponds to the Islamic date 29-08-1432? Then $$j = 1432$$, $$m = 8$$, $$d = 29$$, so

$\begin{split} J \| = \floorratio{10631×1432 − 10617}{30} + \floorratio{325×8 − 320}{11} + 29 + 1948440 \\ \| = \floorratio{15212975}{30} + \floorratio{2280}{11} + 29 + 1948440 \\ \| = 507099 + 207 + 29 + 1948439 \\ \| = 2455774 \end{split}$

The CJDN is 2455774, which corresponds to 31 July 2011 in the Gregorian calendar.

And how about the first day of the first month of the first year of the Islamic calendar? Then $$j = 1$$, $$m = 1$$, $$d = 1$$, so

$\begin{split} J \| = \floorratio{10631×1 − 10617}{30} + \floorratio{325×1 − 320}{11} + 1 + 1948439 \\ \| = \floorratio{14}{30} + \floorratio{5}{11} + 1 + 1948440 = 0 + 0 + 1 + 1948439 = 1948440 \end{split}$

which corresponds to 16 July 622 in the Julian calendar.

### 6.2. From CJDN to Islamic Date

The following formulas calculate the year number $$j$$, month number $$m$$, and day number $$d$$ in the most commonly used administrative Islamic calendar from the CJDN $$J$$:

\begin{align} k_2 \| = 30×(J − 1948440) + 15 \\ k_1 \| = 11\floorratio{k_2 \bmod 10631}{30} + 5 \\ j \| = \floorratio{k_2}{10631} + 1 \\ m \| = \floorratio{k_1}{325} + 1 \\ d \| = \floorratio{k_1 \bmod 325}{11} + 1 \end{align}

For example, which Islamic date corresponds to CJDN 2455774? Then $$J = 2455774$$, and then

\begin{align*} k_2 \| = 30×(2455774 − 1948440) + 15 = 15220035 \\ k_1 \| = 11×\floorratio{15220035 \bmod 10631}{30} + 5 = 11×\floorratio{7074}{30} + 5 = 11×235 + 5 = 2590 \\ j \| = \floorratio{15220035}{10631} + 1 = 1431 + 1 = 1432 \\ m \| = \floorratio{2590}{325} + 1 = 7 + 1 = 8 \\ d \| = \floorratio{2590 \bmod 325}{11} + 1 = \floorratio{315}{11} + 1 = 29 \end{align*}

so $$j = 1432$$, $$m = 8$$, $$d = 29$$ which means 29 Shaban 1432.

## 7. The Babylonian Calendar

See section 13.5 for the derivation of this algorithm.

### 7.1. From Babylonian Date to CJDN

The Babylonians had a lunisolar calendar in which the beginning of each month was determined from observations of the Moon, but the number of months in each year followed a fixed 19-year pattern. The below formulas for an administrative Babylonian calendar uses that same 19-year pattern, and should usually deviate by at most one day from the calendar that the Babylonians used that depended on observations.

If $$j$$ is the year number in the era of Seleukos, and $$m$$ is the month number (beginning at 1) in the current year, and $$d$$ is the day number (beginning at 1) in the current month, then the CJDN $$J$$ can be found as follows:

\begin{align} y_1 \| = \floorratio{235j − 241}{19} + m \\ J \| = \floorratio{6940y_1}{235} + d + 1607557 \end{align}

For example, which CJDN $$J$$ corresponds to year 3, month 9, day 27 of the era of Seleukos? Then $$j = 3$$, $$m = 9$$, $$d = 27$$, so

\begin{align*} x_1 \| = 2 \\ z_1 \| = 8 \\ z_2 \| = 26 \\ c_1 \| = \floorratio{235×2 + 13}{19} = \floorratio{483}{19} = 25 \\ x_2 \| = y_1 = 25 + 8 = 33 \\ c_2 \| = \floorratio{6940×33}{235} = \floorratio{229020}{235} = 974 \\ y_2 \| = 974 + 26 = 1000 \\ J \| = 1000 + 1607558 = 1607658 \end{align*}

### 7.2. From CJDN to Babylonian Date

From CJDN $$J$$, we find the Babylonian year number $$j$$, month number $$m$$, and day number $$d$$ as follows:

\begin{align} k_2 \| = 235×(J − 1607558) + 234 \\ k_1 \| = 19\floorratio{k_2}{6940} + 5 \\ j \| = \floorratio{k_1}{235} + 1 \\ m \| = \floorratio{k_1 \bmod 235}{19} + 1 \\ d \| = \floorratio{k_2 \bmod 6940}{235} + 1 \end{align}

For example, which date in the Babylonian calendar corresponds to CJDN $$1608558$$? Then $$J = 1608558$$, so

\begin{align*} k_2 \| = 235×(1608558 − 1607558) + 234 = 235234 \\ k_1 \| = 19×\floorratio{235234}{6940} + 5 = 19×33 + 5 = 632 \\ j \| = \floorratio{632}{235} + 1 = 3 \\ m \| = \floorratio{632 \bmod 235}{19} + 1 = \floorratio{162}{19} + 1 = 9 \\ d \| = \floorratio{235234 \bmod 6940}{235} + 1 = \floorratio{6214}{235} + 1 = 27 \end{align*}

which means day 27 of month 9 of year 3.

## 8. The Jewish Calendar

See section 13.6 for the derivation of this algorithm.

### 8.1. From Jewish Date to CJDN

The Jewish calendar is a lunisolar calendar with complicated rules, and repeats itself only after 251,827,457 days = 35,975,351 weeks = 8,527,680 months = 689,472 years. The algorithm to transform a Jewish date into CJDN is long, and it features very large intermediate results. The algorithm that I provide here uses only whole numbers (to avoid round-off error), and assumes that all input values, intermediate results, and output values cannot be greater than 231 = 2,147,483,648.

In the Jewish calendar, a year has 12 or 13 months, and a month has 29 or 30 days. New Year is the first day of the 7th month, Tishri. The months in a year of 12 months are: 1 = Nisan (נִיסָן‎), 2 = Iyar (אִיָּר / אייר‎), 3 = Sivan (סִיוָן / סיוון‎), 4 = Tammuz (תַּמּוּז‎), 5 = Av (אָב‎), 6 = Elul (אֱלוּל‎), 7 = Tishri (תִּשׁרִי‎), 8 = Ḥeshvan (מַרְחֶשְׁוָן / מרחשוון‎), 9 = Kislev (כִּסְלֵו / כסליו‎), 10 = Tevet (טֵבֵת‎), 11 = Shevat (שְׁבָט‎), 12 = Adar (אֲדָר‎). In a year of 13 months, embolismic month Adar Ⅰ (‎אֲדָר א׳) is inserted between Shevat and regular Adar, and that regular Adar is then temporarily renamed to Adar Ⅱ (אֲדָר ב׳‎). In a year of 13 months, we count Adar Ⅰ as month number 12, and Adar Ⅱ as month number 13.

One algorithm to transform calendar year $$j$$, calendar month $$m$$ (from 1 through 12 or 13), and calendar day $$d$$ (from 1 through 29 or 30) into CJDN is then:

\begin{align} c_0 \| = \floorratio{13 − m}{7} \\ x_1 \| = j − 1 + c_0 \\ x_3 \| = m − 1 \\ z_4 \| = d − 1 \\ c_1(x_1) \| = \floorratio{235x_1 + 1}{19} \\ q(x_1) \| = \floorratio{c_1(x_1)}{1095} \\ r(x_1) \| = c_1(x_1) \bmod 1095 \\ υ_1(x_1) \| = 32336q(x_1) + \floorratio{15q(x_1) + 765433r(x_1) + 12084}{25920} \\ υ_2(x_1) \| = υ_1(x_1) + \left(\floorratio{6×(υ_1(x_1) \bmod 7)}{7} \bmod 2\right) \end{align}

Calculate in the same way also $$υ_2(x_1 + 1)$$. Then

\begin{align} L_2(x_1) \| = υ_2(x_1 + 1) − υ_2(x_1) \\ L_2(x_1 − 1) \| = υ_2(x_1) − υ_2(x_1 − 1) \\ v_3 \| = 2\left(\floorratio{L_2(x_1) + 19}{15} \bmod 2\right) \\ v_4 \| = \floorratio{L_2(x_1 − 1) + 7}{15} \bmod 2 \\ c_2(x_1) \| = υ_2 + v_3 + v_4 \end{align}

Calculate in the same way also $$c_2(x_1 + 1)$$ (which means you need to calculate $$υ_2(x_1 + 2)$$, too). Then

\begin{align} L \| = c_2(x_1 + 1) − c_2(x_1) \\ c_8 \| = \floorratio{L + 7}{2} \bmod 15 \\ c_9 \| = −\left( \floorratio{385 − L}{2} \bmod 15 \right) \\ c_3 \| = \floorratio{384x_3 + 7}{13} + c_8\floorratio{x_3 + 4}{12} + c_9\floorratio{x_3 + 3}{12} \\ c_4(x_1,x_3) \| = c_2(x_1) + c_3(x_3) \\ J \| = J_0 − 177 + c_4(x_1,x_3) + z_4 = 347821 + c_2(x_1) + c_3 + z_4 \end{align}

As an example, we'll calculate which CJDN corresponds to 18 Sivan A.M. 4682. Then $$j = 4682$$, $$m = 3$$, $$d = 18$$. And then

\begin{align*} c_0 \| = \floorratio{13 − m}{7} \\ \| = \floorratio{13 − 3}{7} \\ \| = \floorratio{10}{7} = 1 \\ x_1 \| = j − 1 + c_0 = 4682 − 1 + 1 = 4682 \\ x_3 \| = m − 1 = 3 − 1 = 2 \\ z_4 \| = d − 1 = 18 − 1 = 17 \\ c_1(4681) \| = c_1(x_1 − 1) = \floorratio{235×(x_1 − 1) + 1}{19} \\ \| = \floorratio{235×4681 + 1}{19} \\ \| = \floorratio{1100036}{19} = 57896 \\ c_1(4682) \| = c_1(x_1) = \floorratio{235×x_1 + 1}{19} \\ \| = \floorratio{235×4682 + 1}{19} \\ \| = \floorratio{1100270 + 1}{19} \\ \| = \floorratio{1100271}{19} = 57909 \\ c_1(4683) \| = c_1(x_1 + 1) = \floorratio{235×(x_1 + 1) + 1}{19} \\ \| = \floorratio{235×4683 + 1}{19} \\ \| = \floorratio{1100506}{19} = 57921 \\ c_1(4684) \| = c_1(x_1 + 2) = \floorratio{235×(x_1 + 2) + 1}{19} \\ \| = \floorratio{235×4684 + 1}{19} \\ \| = \floorratio{1100741}{19} = 57933 \\ q(4681) \| = q(x_1 − 1) = \floorratio{c_1(x_1 − 1)}{1095} \\ \| = \floorratio{57896}{1095} = 52 \\ r(4681) \| = r(x_1 − 1) = c_1(x_1 − 1) \bmod 1095 \\ \| = 57896 \bmod 1095 = 956 \\ υ_1(4681) \| = υ_1(x_1 − 1) = 32336×q(x_1 − 1) \\ \| + \floorratio{15×q(x_1 − 1) + 765433×r(x_1 − 1) + 12084}{25920} \\ \| = 32336×52 + \floorratio{15×52 + 765433×956 + 12084}{25920} \\ \| = 1681472 + \floorratio{731766812}{25920} \\ \| = 1681472 + 28231 = 1709703 \\ υ_2(4681) \| = υ_2(x_1 − 1) = υ_1(x_1 − 1) + \left( \floorratio{6×(υ_1(x_1 − 1) \bmod 7)}{7} \bmod 2\right) \\ \| = 1709703 + \left( \floorratio{6×(1709703 \bmod 7)}{7} \bmod 2 \right) \\ \| = 1709703 + \left( \floorratio{6×2}{7} \bmod 2 \right) \\ \| = 1709703 + \left( \floorratio{12}{7} \bmod 2 \right) \\ \| = 1709703 + (1 \bmod 2) = 1709703 + 1 = 1709704 \\ q(4682) \| = q(x_1) = \floorratio{c_1(x_1)}{1095} \\ \| = \floorratio{57909}{1095} = 52 \\ r(4682) \| = r(x_1) = c_1(x_1) \bmod 1095 = 57909 \bmod 1095 = 969 \\ υ_1(4682) \| = υ_1(x_1) = 32336×q(x_1) \\ \| + \floorratio{15×q(x_1) + 765433×r(x_1) + 12084}{25920} \\ \| = 32336×52 + \floorratio{15×52 + 765433×969 + 12084}{25920} \\ \| = 1681472 + \floorratio{741717441}{25920} \\ \| = 1681472 + 28615 = 1710087 \\ υ_2(4682) \| = υ_2(x_1) = υ_1(x_1) + \left( \floorratio{6×(υ_1(x_1) \bmod 7)}{7} \bmod 2 \right) \\ \| = 1710087 + \left( \floorratio{6×(1710087 \bmod 7)}{7} \bmod 2 \right) \\ \| = 1710087 + \left( \floorratio{1}{7} \bmod 2 \right) = 1710087 + (0 \bmod 2) \\ \| = 1710087 + 0 = 1710087 \\ q(4683) \| = q(x_1 + 1) = \floorratio{c_1(x_1 + 1)}{1095} \\ \| = \floorratio{57921}{1095} = 52 \\ r(4683) \| = r(x_1 + 1) = c_1(x_1 + 1) \bmod 1095 \\ \| = 57921 \bmod 1095 = 981 \\ υ_1(4683) \| = υ_1(x_1 + 1) = 32336×q(x_1 + 1) \\ \| + \floorratio{15×q(x_1 + 1) + 765433×r(x_1 + 1) + 12084}{25920} \\ \| = 332336×52 + \floorratio{15×52 + 765433×981 + 12084}{25920} \\ \| = 17281472 + \floorratio{750902637}{25920} \\ \| = 17281472 + 28970 = 17310442 \\ υ_2(4683) \| = υ_2(x_1 + 1) = υ_1(x_1 + 1) \\ \| + \left( \floorratio{6×(υ_1(x_1 + 1) \bmod 7}){7} \bmod 2 \right) \\ \| = 1710442 + \left( \floorratio{6×(1710442 \bmod 7)}{7} \bmod 2 \right) \\ \| = 1710442 + \left( \floorratio{6×6}{7} \bmod 2 \right) \\ \| = 1710442 + \left( \floorratio{36}{7} \bmod 2 \right) \\ \| = 1710442 + (5 \bmod 2) = 1710442 + 1 = 1710443 \\ q(4684) \| = q(x_1 + 2) = \floorratio{c_1(x_1 + 2)}{1095} \\ \| = \floorratio{57933}{1095} = 52 \\ r(4684) \| = r(x_1 + 2) = c_1(x_1 + 2) \bmod 1095 \\ \| = 57933 \bmod 1095 = 993 \\ υ_1(4684) \| = υ_1(x_1 + 2) = 32336×q(x_1 + 2) \\ \| + \floorratio{15×q(x_1 + 2) + 765433×r(x_1 + 2) + 12084}{25920} \\ \| = 32336×52 + \floorratio{15×52 + 765433×993 + 12084}{25920} \\ \| = 1681472 + \floorratio{760087833}{25920} \\ \| = 1681472 + 29324 = 1710796 \\ υ_2(4684) \| = υ_2(x_1 + 2) = υ_1(x_1 + 2) + \left( \floorratio{6×(υ_1(x_1 + 2) \bmod 7)}{7} \bmod 2 \right) \\ \| = 1710796 + \left( \floorratio{6×(1710796 \bmod 7)}{7} \bmod 2 \right) \\ \| = 1710796 + \left( \floorratio{6×3}{7} \bmod 2 \right) \\ \| = 1710796 + \left( \floorratio{18}{7} \bmod 2 \right) = 1710796 + (2 \bmod 2) \\ \| = 1710796 + 0 = 1710796 \\ L_2(4681) \| = L_2(x_1 − 1) = υ_2(x_1) − υ_2(x_1 − 1) \\ \| = 1710087 − 1709704 = 383 \\ L_2(4682) \| = L_2(x_1) = υ_2(x_1 + 1) − υ_2(x_1) \\ \| = 1710443 − 1710087 = 356 \\ L_2(4683) \| = L_2(x_1 + 1) = υ_2(x_1 + 2) − υ_2(x_1 + 1) \\ \| = 1710796 − 1710443 = 353 \\ v_3(4682) \| = v_3(x_1) = 2×\left( \floorratio{L_2(x_1) + 19}{15} \bmod 2 \right) \\ \| = 2×\left( \floorratio{356 + 19}{15} \bmod 2 \right) \\ \| = 2×\left( \floorratio{375}{15} \bmod 2 \right) \\ \| = 2×(25 \bmod 2) = 2 \\ v_4(4682) \| = v_4(x_1) = \floorratio{L_2(x_1 − 1) + 7}{15} \bmod 2 \\ \| = \floorratio{383 + 7}{15} \bmod 2 \\ \| = \floorratio{390}{15} \bmod 2 = 26 \bmod 2 = 0 \\ c_2(4682) \| = c_2(x_1) = υ_2(x_1) + v_3(x_1) + v_4(x_1) \\ \| = 1710087 + 2 + 0 = 1710089 \end{align*}

\begin{align*} v_3(4683) \| = v_3(x_1 + 1) = 2×\left( \floorratio{L_2(x_1 + 1) + 19}{15} \bmod 2 \right) \\ \| = 2×\left( \floorratio{353 + 19}{15} \bmod 2 \right) \\ \| = 2×\left( \floorratio{372}{15} \bmod 2 \right) \\ \| = 2×(24 \bmod 2) = 0 \\ v_4(4683) \| = v_4(x_1 + 1) = \floorratio{L_2(x_1) + 7}{15} \bmod 2 \\ \| = \floorratio{356 + 7}{15} \bmod 2 \\ \| = \floorratio{363}{15} \bmod 2 = 24 \bmod 2 = 0 \\ c_2(4683) \| = c_2(x_1 + 1) = υ_2(x_1 + 1) + v_3(x_1 + 1) + v_4(x_1 + 1) = 1710443 \\ L \| = y_2(x_1 + 1) − y_2(x_1) = 1710443 − 1710089 = 354 \\ c_8 \| = \floorratio{L + 7}{2} \bmod 15 \\ \| = \floorratio{354 + 7}{2} \bmod 15 \\ \| = \floorratio{361}{2} \bmod 15 = 180 \bmod 15 = 0 \\ c_9 \| = −\left( \floorratio{385 − L}{2} \bmod 15 \right) \\ \| = −\left( \floorratio{385 − 354}{2} \bmod 15 \right) \\ \| = −\left( \floorratio{31}{2} \bmod 15 \right) \\ \| = −(15 \bmod 15) = 0 \\ c_3 \| = \floorratio{384×x_3 + 7}{13} + c_8×\floorratio{x_3 + 4}{12} + c_9×\floorratio{x_3 + 3}{12} \\ \| = \floorratio{384×2 + 7}{13} + 0×\floorratio{2 + 4}{12} + 0×\floorratio{2 + 3}{12} \\ \| = \floorratio{775}{13} + 0 + 0 = 59 \\ c_4 \| = c_2 + c_3 = 1710089 + 59 = 1710148 \\ J \| = J_0 − 177 + c_4 + z_4 = 347821 + 1710148 + 17 = 2057986 \end{align*}

This date corresponds to 17 June 922 in the Julian calendar.

### 8.2. From CJDN to Jewish Calendar

From CJDN $$J$$ we go to Jewish year $$j$$, month $$m$$, and day $$d$$:

\begin{align} y_4 \| = J − 347821 \\ q \| = \floorratio{y_4}{1447} \\ r \| = y_4 \bmod 1447 \\ y_1' \| = 49q + \floorratio{23q + 25920r + 13835}{765433} \\ γ_1 \| = y_1' + 1 \\ ξ_1 \| = \floorratio{19γ_1 + 17}{235} \\ μ_1 \| = γ_1 − \floorratio{235ξ_1 + 1}{19} \end{align}

Calculate $$c_{41}' = c_4(ξ_1, μ_1)$$ in the way described in the previous section. Then

\begin{align} ζ_1 \| = y_4 − c_{41}' \\ γ_2 \| = γ_1 + \floorratio{ζ_1}{33} \\ ξ_2 \| = \floorratio{19γ_2 + 17}{235} \\ μ_2 \| = γ_2 − \floorratio{235ξ_2 + 1}{19} \end{align}

Calculate $$c_{42}' = c_4(ξ_2, μ_2)$$ in the way described in the previous section. Then

\begin{align} ζ_2 \| = y_4 − c_{42}' \\ γ_3 \| = γ_2 + \floorratio{ζ_1}{33} \\ x_1 \| = ξ_3 = \floorratio{19γ_3 + 17}{235} \\ x_3 \| = μ_3 = γ_3 − \floorratio{235ξ_3 + 1}{19} \end{align}

Calculate $$c_{43}' = c_4(ξ_3, μ_3)$$ in the way described in the previous section. Then

\begin{align} z_4 \| = ζ_3 = y_1 − c_{43}' \\ c \| = \floorratio{12 − x_3}{7} \\ j \| = x_1 + 1 − c \\ m \| = x_3 + 1 \\ d \| = z_4 + 1 \end{align}

For $$J = 2057986$$ we find

\begin{align*} y_4 \| = J − 347821 = 2057986 − 347821 = 1710165 \\ q \| = \floorratio{y_4}{1447} = \floorratio{1710165}{1447} = 1181 \\ r \| = y_4 \bmod 1447 = 1710165 \bmod 1447 = 1258 \\ y_1' \| = 49q + \floorratio{23q + 25920r + 13835}{765433} \\ \| = 49×1181 + \floorratio{23×1181 + 25920×1258 + 13835}{765433} \\ \| = 57869 + \floorratio{32648358}{765433} = 57869 + 42 = 57911 \\ γ_1 \| = y_1' + 1 = 57911 + 1 = 57912 \\ ξ_1 \| = \floorratio{19γ_1 + 17}{235} \\ \| = \floorratio{19×57912 + 17}{235} \\ \| = \floorratio{1100345}{235} = 4682 \\ μ_1 \| = γ_1 − \floorratio{235ξ_1 + 1}{19} \\ \| = 57912 − \floorratio{235×4682 + 1}{19} \\ \| = 57912 − \floorratio{1100271}{19} = 57912 − 57909 = 3 \end{align*}

In the way described in the previous section we calculate that

$c_{41}' = c_4(ξ_1,μ_1) = c_4(4682,3) = 1710178$

Then

\begin{align*} ζ_1 \| = y_4 − c_{41}' = 1710165 − 1710178 = −13 \\ γ_2 \| = γ_1 + \floorratio{ζ_1}{33} = 57912 + \floorratio{−13}{33} = 57912 + −1 = 57911 \\ ξ_2 \| = \floorratio{19γ_2 + 17}{235} = \floorratio{19×57911 + 17}{235} = \floorratio{1100326}{235} = 4682 \\ μ_2 \| = γ_2 − \floorratio{235ξ_2 + 1}{19} = 57911 − \floorratio{235×4682 + 1}{19} \\ \| = 57911 − \floorratio{1100271}{19} = 57911 − 57909 = 2 \end{align*}

In the way described in the previous section we calculate that

$c_{42} = c_4(ξ_2,μ_2) = c_4(4682,2) = 1710148$

Then

\begin{align*} ζ_2 \| = y_4 − c_{42}' = 1710165 − 1710148 = 17 \\ γ_3 \| = γ_2 + \floorratio{ζ_2}{33} = 57911 + \floorratio{17}{33} = 57911 + 0 = 57911 \end{align*}

Because in this example $$ζ_3 = ζ_2$$, also $$ξ_3 = ξ_2$$, $$μ_3 = μ_2$$, $$c_4(ξ_3,μ_3) = c_4(ξ_2,μ_2)$$, and $$ζ_3 = ζ_2$$, so in this example you need not calculate $$ξ_3$$, $$μ_3$$, $$c_4(ξ_3,μ_3)$$, $$ζ_3$$ explicitly, but you can still do so if that is more convenient.

\begin{align*} x_1 \| = ξ_3 = \floorratio{19γ_3 + 17}{235} = \floorratio{19×57911 + 17}{235} = \floorratio{1100326}{235} = 4682 \\ x_3 \| = μ_3 = γ_3 − \floorratio{235ξ_3 + 1}{19} = 57911 − \floorratio{235×4682 + 1}{19} \\ \| = 57911 − \floorratio{1100271}{19} = 57911 − 57909 = 2 \end{align*}

In the way described in the previous section we calculate that

$c_{43}' = c_4(ξ_3,μ_3) = c_4(4682,2) = 1710148$

Then

\begin{align*} z_4 \| = ζ_3 = y_4 − c_{43}' = 1710165 − 1710148 = 17 \\ c \| = \floorratio{12 − x_3}{7} = \floorratio{12 − 2}{7} = \floorratio{10}{7} = 1 \\ j \| = x_1 + 1 − c = 4682 + 1 − 1 = 4682 \\ m \| = x_3 + 1 = 2 + 1 = 3 \\ d \| = z_4 + 1 = 17 + 1 = 18 \end{align*}

so the desired date in the Jewish calendar is day 18 of month 3 (Sivan) of year 4682: 18 Sivan A.M. 4682.

## 9. The Egyptian Calendar

The ancient Egyptians had a very simple calendar indeed, without leap years and with 30 days to every month, except that the last month had 5 days. In the calendar according to the Era of Nabonassar the first day (1 Thoth of year 1) was equivalent to 26 February −746 in the Julian calendar.

### 9.1. From Egyptian Date to CJDN

The algorithm to translate an Egyptian date (calendar year $$j$$, calendar month $$m$$, calendar day $$d$$) to CJDN $$J$$ is as follows:

\begin{equation} J = 365 j + 30 m + d + 1448242 \end{equation}

For example, which CJDN corresponds to day 7 of month 5 of year 218 in the Egyptian calendar? Then we find

\begin{align*} J \| = 365j + 30m + d + 1448242 \\ \| = 365×218 + 30×5 + 7 + 1448242 \\ \| = 79570 + 150 + 7 + 1448242 = 1527969 \end{align*}

so the answer is CJDN 1527969.

### 9.2. From CJDN to Egyptian Date

In the opposite direction, the calculations are simple as well.

\begin{align} y_2 \| = J − 1448638 \\ x_2 \| = \floorratio{y_2}{365} \\ y_1 \| = y_2 \bmod 365 \\ j \| = x_2 + 1 \\ m \| = \floorratio{y_1}{30} + 1 \\ d \| = y_1 − 30m + 31 = (y_1 \bmod 30) + 1 \end{align}

What date in the Egyptian calendar corresponds to CJDN 1527969? Then

\begin{align*} y_2 \| = J − J_0 = 1527969 − 1448638 = 79331 \\ x_2 \| = \floorratio{y_2}{365} = \floorratio{79331}{365} = 217 \\ y_1 \| = y_2 − 365x_2 = 79331 − 365×217 = 79331 − 79205 = 126 \\ j \| = x_2 + 1 = 217 + 1 = 218 \\ m \| = \floorratio{y_1}{30} + 1 = \floorratio{126}{30} + 1 = 4 + 1 = 5 \\ d \| = y_1 − 30m + 31 = 126 − 30×5 + 31 = (y_1 \bmod 30) + 1 = (126 \bmod 30) + 1 = 6 + 1 = 7 \end{align*}

so it is day 7 of month 5 of year 218.

## 10. The Maya Calendar

### 10.1. From Maya Calendar to CJDN

The Maya from Anahuac (Central America) used three different calendars, of which two (the Tzolkin and Haab) were periodic with fairly short periods, and the third one (the Long Count) might have been intended to be periodic but has some periods that are so long that it can be considered to be continuous rather than repeating.

Different areas in Central America had slightly different versions of these calendars, with different names for days and months, and different ways to indicate years, and sometimes days were counted from 0 rather than from 1. Below we describe the calendars from the city of Tikal.

#### 10.1.1. From Haab to CJDN

The Haab has a day number and a month, but no year number. There are 18 months of 20 days, plus a 19th month of 5 days, which makes 365 days in total, which we call a Haab year. There are no leap years. The months have a name, and the days have a number beginning at 0.

We write a date in the Haab as $$\{h_d,h_m\}$$ where $$h_d$$ is the day number (from 0 through 19) and $$h_m$$ is the month number (from 1 through 19).

There is no year number in the Haab, so a particular Haab date $$\{h_d,h_m\}$$ returns every 365 days. We need to apply additional information to pick the right one. We can find the last CJDN $$J$$ on or before a particular CJDN $$J_0$$ that corresponds to this Haab date, as follows:

\begin{align} H \| = h_d + 20×(h_m − 1) \\ J \| = J_0 − ((J_0 − H + 65) \bmod 365) \end{align}

What is the last CJDN in the year 1965 that corresponds to Haab date $$\{5,13\}$$? Then $$h_d = 5$$, $$h_m = 13$$, $$J_0 = 2439126$$ (which corresponds to 31 December 1965), so

\begin{align*} H \| = 5 + 20×(13 − 1) = 245 \\ J \| = J_0 − ((J_0 − H + 65) \bmod 365) \\ \| = 2439126 − (2438946 \bmod 365) \\ \| = 2439126 − 16 = 2439110 \end{align*}

#### 10.1.2. From Tzolkin to CJDN

The Tzolkin has a period of 20 days (the venteina) with a name for each day, and a period of 13 days (the trecena) with a number for each day (beginning at 1).

We write a date in the Tzolkin as $$\{t_t,t_v\}$$, where $$t_t$$ is the day number in the trecena (from 1 through 13) and $$t_v$$ is the day number in the venteina (from 1 through 20).

The trecena and venteina increase simultaneously, so after day $$\{4,7\}$$ comes day $$\{5,8\}$$, and then $$\{6,9\}$$, and so on. Because 13 and 20 are relatively prime, all possible combinations of venteina and trecena occur in this calendar, so after 13×20 = 260 days the dates repeat again.

There is no year number in the Tzolkin so a particular Tzolkin date returns every 260 days. We can find the last CJDN $$J$$ that corresponds to Tzolkin date $$\{t_t,t_v\}$$ on or before a particular CJDN $$J_0$$ as follows:

\begin{equation} J = J_0 − ((J_0 − 40 t_t + 39 t_v + 97) \bmod 260) \end{equation}

The last Tzolkin date $$\{4,7\}$$ that occurs on or before 31 December 1965 (= CJDN 2439126) is

\begin{align*} J \| = 2439126 − ((2439126 − 40×4 + 39×7 + 97) \bmod 260) \\ \| = 2439126 − (2439336 \bmod 260) \\ \| = 2439126 − 16 = 2439110 \end{align*}

For the number of days $$T$$ since the last $$\{1,1\}$$ we have

\begin{equation} T = (40 t_t + 221 t_v − 1) \bmod 260 \end{equation}

For CJDN 2439110 (with $$\{t_t,t_v\} = \{4,7\}$$) we find

\begin{align*} T \| = (40×4 + 221×7 − 1) \bmod 260 \\ \| = 1706 \bmod 260 = 146 \end{align*}

so the last day before then that had $$\{1,1\}$$ was CJDN $$J = 2439110 − 146 = 2438964$$, and that fits, because $$t_t = ((J + 5) \bmod 13) + 1 = (2438969 \bmod 13) + 1 = 1$$ and $$t_v = ((J + 16) \bmod 20) + 1 = (2438980 \bmod 20) + 1 = 1$$.

#### 10.1.3. From Tzolkin and Haab to CJDN

Sometimes a date is indicated in both Tzolkin and Haab. The CJDN $$J$$ of the last Tzolkin/Haab date $$\{ h_d, h_m, t_t, t_v \}$$ on or before CJDN $$J_0$$ is:

\begin{align} H \| = (h_d + 20 × (h_m − 1)) \bmod{365} \\ T \| = (40t_t + 221 t_v − 1) \bmod{260} \\ J \| = J_0 − ((J_0 − 365 T + 364 H − 7600) \bmod{18980}) \end{align}

The Tzolkin and Haab together have a period of 18980 days, which is approximately 52 years.

Which CJDN is the last one before the end of 1965 that corresponds to $$\{t_t,t_v,h_d,h_m\} = \{4,7,5,13\}$$? Then $$J_0 = 2439126$$ and

\begin{align*} H \| = (5 + 20×12) \bmod{365} = 245 \bmod{365} = 245 \\ T \| = (40×4 + 221×7 − 1) \bmod{260} = 1706 \bmod{260} = 146 \\ J \| = 2439126 − ((2439126 − 365×146 + 364×245 − 7600) \bmod{18980}) \\ \| = 2439126 − (2467416 \bmod{18980}) \\ \| = 2439126 − 16 = 2439110 \end{align*}

Beware! Not all possible combinations of $$\{ h_d, h_m, t_t, t_v \}$$ occur in the calendar. You can fill in any combination you like in the preceding formulas, but you'll get useful results only for combinations that really occur in the calendar. Those are combinations for which $$H − T ≡ 65 − 96 ≡ 4 \pmod{5}$$.

In the example that we've been using we have $$H = 245$$ and $$T = 146$$, so $$H − T = 99 ≡ 4 \pmod{5}$$, so this combination indeed occurs in this calendar.

#### 10.1.4. From the Long Count to CJDN

The Long Count counts days and has a series of increasingly longer periods. The smallest period is 20 days, the next one is 18 times longer (i.e., 360 days), and after that each next period is 20 times longer than the previous one. The number for each period begins at 0. Often dates in the Long Count are given with five numbers, but even longer periods are known, up to nine numbers. The longest known period corresponds to about 63 million years. Here we assume that the Long Count has five numbers, and that the fifth number can get arbitrarily long.

The epoch (0.0.0.0.0) of the Long Count probably corresponds to CJDN $$J_0 = 584283$$ (6 September −3113 in the Julian calendar).

Translating a Long Count to CJDN goes as follows:

\begin{equation} \begin{split} J \| = l_1 + 20×(l_2 + 18×(l_3 + 20×(l_4 + 20×l_5))) + J_0 \\ \| = l_1 + 20×l_2 + 360×l_3 + 7200×l_4 + 144000×l_5 \end{split} \end{equation}

Which CJDN corresponds to Long Count $$L = 12.17.12.5.7$$? Then

$J = 7 + 20×(5 + 18×(12 + 20×(17 + 20×12))) + 584283 = 2439110$

### 10.2. From CJDN to the Maya Calendar

#### 10.2.1. From CJDN to Haab

We calculate Haab date $$\{h_d,h_m\}$$ from CJDN $$J$$ as follows:

\begin{align} H \| = (J + 65) \bmod 365 \\ h_m \| = \floorratio{H}{20} + 1 \\ h_d \| = H \bmod 20 \end{align}

$$H$$ is the number of days since the beginning of the current Haab year (with $$H = 0$$ for the first day of the Haab year).

For example, which Haab date corresponds to 15 December 1965 = CJDN 2439110? Then $$J = 2439110$$ and

\begin{align*} H \| = (J + 65) \bmod 365 = 2439175 \bmod 365 = 245 \\ h_m \| = \floorratio{245}{20} + 1 = 13 \\ h_d \| = 245 \bmod 20 = 5 \end{align*}

so that day is 245 days since the beginning of the current Haab year and corresponds to the 5th day of the 13th month, i.e., $$\{5,13\}$$.

#### 10.2.2. From CJDN to Tzolkin

We translate CJDN $$J$$ to a Tzolkin date as follows:

\begin{align} t_t \| = ((J + 5) \bmod{13}) + 1 \label{eq:trecena} \\ t_v \| = ((J + 16) \bmod{20}) + 1 \label{eq:venteina} \end{align}

What is the Tzolkin date corresponding to CJDN 2439110? Then

\begin{align*} t_t \| = ((2439110 + 5) \bmod{13}) + 1 = 3 + 1 = 4 \\ t_v \| = ((2439110 + 16) \bmod{20}) + 1 = 6 + 1 = 7 \end{align*}

so that date is $$\{4,7\}$$.

#### 10.2.3. From CJDN to the Long Count

Translating CJDN $$J$$ to Long Count $$L ≡ l_5.l_4.l_3.l_2.l_1$$ goes as follows:

\begin{align} x_5 \| = J − J_0 \\ l_5 \| = \floorratio{x_5}{144000} \\ x_4 \| = x_5 \bmod 144000 \\ l_4 \| = \floorratio{x_4}{7200} \\ x_3 \| = x_4 \bmod 7200 \\ l_3 \| = \floorratio{x_3}{360} \\ x_2 \| = x_3 \bmod 360 \\ l_2 \| = \floorratio{x_2}{20} \\ l_1 \| = x_2 \bmod 20 \end{align}

Which Long Count $$L$$ corresponds to CJDN $$J = 2439110$$? Then

\begin{align*} x_5 \| = J − J_0 = 2439110 − 584283 = 1854827 \\ l_5 \| = \floorratio{1854827}{144000} = 12 \\ x_4 \| = 1854827 \bmod 144000 = 126827 \\ l_4 \| = \floorratio{126827}{7200} = 17 \\ x_3 \| = 126827 \bmod 7200 = 4427 \\ l_3 \| = \floorratio{4427}{360} = 12 \\ x_2 \| = 4427 \bmod 360 = 107 \\ l_2 \| = \floorratio{107}{20} = 5 \\ l_1 \| = 107 \bmod 20 = 7 \end{align*}

The answer is $$L = 12.17.12.5.7$$.

### 10.3. A LunarCalendar With Many Fixed Month Lengths

I've designed a lunisolar calendar in which nearly all months have the same number of days in every calendar year. The calendar follows the seasons by means of the Cycle of Meton, which lasts 19 years. Each year contains 12 or 13 months. The "shortest" years of 12 months contain 354 days. The "short" years of 12 months contain 355 days. The "long" years of 13 months contain 384 days. The year lengths are:

Year Months Days Type
1 12 354 shortest
2 12 355 short
3 13 384 long
4 12 354 shortest
5 12 354 shortest
6 13 384 long
7 12 355 short
8 13 384 long
9 12 354 shortest
10 12 354 shortest
11 13 384 long
12 12 355 short
13 12 354 shortest
14 13 384 long
15 12 354 shortest
16 12 355 short
17 13 384 long
18 12 354 shortest
19 13 384 long

All together there are 235 months (125 long, 110 short) and 6940 days in the cycle of 19 years.

All odd months (except for the 13th) contain 30 days, and all even months contain 29 days, except that in short and long years (of 355 and 384 days) the 12th month contains 30 days instead of 29.

354 355 384
Month Days
1 30 30 30
2 29 29 29
3 30 30 30
4 29 29 29
5 30 30 30
6 29 29 29
7 30 30 30
8 29 29 29
9 30 30 30
10 29 29 29
11 30 30 30
12 29 30 30
13 29

#### 10.3.1. From LunarCalendar to Running Day Number

Given year $$j$$, month $$m$$, and day $$d$$ in this lunar calendar, the running day number $$y_2$$ can be calculated as follows:

\begin{equation} y_2 = 354j + 29m + 30\floorratio{7j − 6}{19} + \floorratio{4j + 8}{19} + \floorratio{7m}{13} + d − 384 \end{equation}

For example, which running day number corresponds to day 27 of month 9 of year 3 in this calendar? Then

\begin{align*} j \| = 3 \\ m \| = 9 \\ d \| = 27 \\ y_2 \| = 354×3 + 29×9 + 30\floorratio{7×3 − 6}{19} + \floorratio{4×3 + 8}{19} + \floorratio{7×9}{13} + 27 − 384 \\ \| = 1323 + 30\floorratio{15}{19} + \floorratio{20}{19} + \floorratio{63}{13} − 357 \\ \| = 1323 + 0 + 1 + 4 − 357 = 971 \end{align*}

#### 10.3.2. From Running Day Number to LunarCalendar

Given running day number $$y_2$$, we calculate year $$j$$, month $$m$$, and day $$d$$ in this lunar calendar as follows:

\begin{align} x' \| = \floorratio{19y_2 + 546}{6940} \\ x_2 \| = x' + \floorratio{y_2 − 354x' − 30\floorratio{7x' + 1}{19} − \floorratio{4x' + 12}{19}}{385} \\ k_1 \| = 13×\left( y_2 − 354x_2 − 30\floorratio{7x_2 + 1}{19} − \floorratio{4x_2 + 12}{19} \right) + 5 \\ j \| = x_2 + 1 \\ m \| = \floorratio{k_1}{384} + 1 \\ d \| = \floorratio{k_1 \bmod 384}{13} + 1 \end{align}

For example, which day in this calendar corresponds to running day number 971? Then $$y_2 = 971$$ and then

\begin{align*} x' \| = \floorratio{19×971 + 546}{6940} = \floorratio{18995}{6940} = 2 \\ x_2 \| = 2 + \floorratio{971 − 354×2 − 30 \floorratio{7×2 + 1}{19} − \floorratio{4×2 + 12}{19}}{385} \\ \| = 2 + \floorratio{263 − 30 \floorratio{15}{19} − \floorratio{20}{19}}{385} \\ \| = 2 + \floorratio{263 − 0 − 1}{385} = 2 \\ k_1 \| = 13×\left( 971 − 354×2 − 30\floorratio{7×2 + 1}{19} − \floorratio{4×2 + 12}{19} \right) + 9 \\ \| = 13×\left( 263 − 30\floorratio{15}{19} − \floorratio{20}{19} \right) + 9 \\ \| = 13×(263 − 0 − 1) + 5 = 3411 \\ j \| = 2 + 1 = 3 \\ m \| = \floorratio{3411}{384} + 1 = 9 \\ d \| = \floorratio{3411 \bmod 384}{13} + 1 = \floorratio{339}{13} + 1 = 27 \end{align*}

so the date is day 27 of month 9 of year 3.

## 11. The Chinese Calendar

The Chinese calendar is a lunisolar calendar. The date of Chinese New Year depends on the position of the Sun and the Moon, but a precise method for calculating those positions has not been fixed. Here I discuss a few characteristics of the Chinese calendar.

### 11.1. The Year of the Monkey

In the traditional Chinese calendar, each year has a name from a cycle of 10 celestial stems and a name from a cycle of 12 terrestrial branches. For each next year both the celestial stem and terrestrial branch advance by one. The (untranslatable) celestial stems are 1. jiǎ, 2. yǐ, 3. bǐng, 4. dīng, 5. wù, 6. jǐ, 7. gēng, 8. xīn, 9. rén, 10. guǐ. The terrestrial branches are 1. zǐ (rat), 2. chǒu (ox), 3. yín (tiger), 4. mǎo (rabbit), 5. chén (dragon), 6. sì (snake), 7. wǔ (horse), 8. wèi (goat), 9. shēn (monkey), 10. yǒu (rooster), 11. xū (dog), 12. hài (pig).

You can calculate the celestial stem $$s$$ (1 through 10) and terrestrial branch $$b$$ (1 through 12) from the (astronomical) Gregorian year number $$j$$ as follows:

\begin{align} s \| = ((j + 6) \bmod 10) + 1 \label{eq:jnaarchinees} \\ b \| = ((j + 8) \bmod 12) + 1 \end{align}

The number $$n$$ in the combined cycle of 60 years is

\begin{equation} n = ((j + 56) \bmod 60) + 1 \end{equation}

For example, for the year 2000 in the Gregorian calendar:

\begin{align*} s \| = ((2000 + 6) \bmod 10) + 1 = 6 + 1 = 7 \\ b \| = ((2000 + 8) \bmod 12) + 1 = 4 + 1 = 5 \\ n \| = ((2000 + 56) \bmod 60) + 1 = 16 + 1 = 17 \end{align*}

so early in the Gregorian year 2000 a Chinese Year of the Metal Dragon began, of the gēng branch (gēngchén).

The years that have a particular celestial stem $$s$$ or terrestrial branch $$b$$ or combination $$n$$ are

\begin{align} j \| ≡ 1983 + s \pmod{10} \\ j \| ≡ 1983 + b \pmod{12} \\ j \| ≡ 1983 + n \pmod{60} \end{align}

The years that have celestial stem $$s$$ as well as terrestrial branch $$b$$ are

\begin{equation} j ≡ 1983 + 25b − 24s \pmod{60} \label{eq:chineesnaarj} \end{equation}

For example, for $$s = 7$$ we find

$j ≡ 1983 + 7 = 1990 \pmod{10}$

so 1990 and every 10 years earlier or later, so 2000 fits, too.

For $$b = 5$$ we find

$j ≡ 1983 + 5 = 1988 \pmod{12}$

so 1988 and every 12 years earlier or later, so 2000 fits, too.

For $$s = 7$$ and $$b = 5$$ we find

$j ≡ 1983 + 25×5 − 24×7 = 1940 \pmod{60}$

so 1940 and every 60 years earlier or later, so 2000 fits, too.

Beware! Not every combination of $$s$$ and $$b$$ is valid! Only combinations for which $$s ≡ b \pmod{2}$$ are valid, so the difference between $$s$$ and $$b$$ must be even.

For example, the combination $$s = 3$$ and $$b = 6$$ does not occur in this calendar, so is not valid. If you enter that combination into equation \ref{eq:chineesnaarj} then you get an answer that looks reasonable: $$j ≡ 1983 + 25×6 − 24×3 = 2061 \pmod{60}$$, but if you enter $$j = 2061$$ into equations \ref{eq:jnaarchinees}ff then you get $$s = 8$$ and $$b = 6$$, a different (and valid) combination than the one that you started with.

### 11.2. HYSN

The HYSN system is a special way to assign a number to a year. The conversion of a Gregorian (or Julian proleptic) year number to a HYSN year number goes as follows:

\begin{align} \{ H − 1, j_2 \} \| = \Div(j + 67016, 10800) \\ \{ Y − 1, j_3 \} \| = \Div(j_2, 360) \\ \{ S − 1, N − 1 \} \| = \Div(j_3, 30) \end{align}

For example, which HYSN year corresponds approximately to our year 2016? Then

\begin{align*} j \| = 2016 \\ \{ H − 1, j_2 \} \| = \Div(2016 + 67016, 10800) = \Div(69032, 10800) = \{ 6, 4232 \} \\ H \| = 7 \\ j_2 \| = 4232 \\ \{ Y − 1, j_3 \} \| = \Div(4232, 360) = \{ 11, 272 \} \\ Y \| = 12 \\ j_3 \| = 272 \\ \{ S − 1, N − 1 \} \| = \Div(272, 30) = \{ 9, 2 \} \\ S \| = 10 \\ N \| = 3 \end{align*}

so the HYSN year number is 0712-1003.

In the other direction things are simpler. Given $$H$$, $$Y$$, $$S$$, and $$N$$, the Gregorian year is

\begin{equation} j = H×10800 + Y×360 + S×30 + N − 78207 \end{equation}

For example, HYSN 0712-1003 means

\begin{align*} H \| = 7 \\ Y \| = 12 \\ S \| = 10 \\ N \| = 3 \\ j \| = 7×10800 + 12×360 + 10×30 + 3 − 78207 \\ \| = 75600 + 4320 + 300 + 3 − 78207 = 2016 \end{align*}

## 12. Derivation of the General Algorithms

We can derive many calendar formulas by finding straight lines that, with rounding, yield the correct results.

### 12.1. Notation

We use special notation for some things that are useful later.

$$C(x)$$ has value 1 if test $$x$$ is satisfied, and 0 otherwise.

$$x∥y$$ asserts that $$x$$ is a multiple of $$y$$.

$$x⊥y$$ asserts that $$x$$ is not a multiple of $$y$$.

$$⌊x⌋$$ is the floor function, which returns the nearest whole number not exceeding $$x$$ (i.e., in the direction of $$−∞$$).

\begin{equation} ⌊x⌉_y ≡ x \bmod y ≡ x − y\floorratio{x}{y} \end{equation}

is the modulus function that yields the non-negative difference between $$x$$ and the nearest multiple of $$y$$ not exceeding $$x$$. Always

\begin{align} x \| = ⌊x⌋ + ⌊x⌉_1 \\ x \| = y\floorratio{x}{y} + ⌊x⌉_y \\ ⌊x⌉_y \| = y\left\lfloor \frac{x}{y} \right\rceil_1 \end{align}

$$⌈x⌉$$ is the ceiling function that yields the nearest whole number not less than $$x$$ (i.e., in the direction of $$+∞$$).

\begin{equation} ⌈x⌋_y ≡ x \bdom y ≡ y\ceilratio{x}{y} − x \end{equation}

is the domulus function that yields the non-negative difference between $$x$$ and the nearest multiple of $$y$$ not less than $$x$$. I made up the notation $$\bdom$$, which is $$\bmod$$ spelled backwards. And I invented the name "domulus", like "modulus" but with "dom" instead of "mod". Always

\begin{align} x \| = ⌈x⌉ − ⌈x⌋_1 \\ x \| = y\ceilratio{x}{y} − ⌈x⌋_y \\ ⌈x⌋_y \| = y\left\lceil \frac{x}{y} \right\rfloor_1 \end{align}

$$\{ m, n \} = \Div(x,y)$$ is a function that yields two values $$m, n$$ such that $$x = my + n$$ with $$m$$ a whole number, $$0 ≤ n \lt y > 0$$, and

\begin{align*} m \| = \floorratio{x}{y} \\ n \| = ⌊x⌉_y = x \bmod y \end{align*}

A few useful relationships:

\begin{align} ⌈x⌉ − ⌊x⌋ \| = C(x⊥1) \\ ⌈x⌋_p + ⌊x⌉_p \| = pC(x⊥p) \\ ⌊−x⌋ \| = −⌈x⌉ = −⌊x⌋ − C(x⊥1) \\ ⌈−x⌉ \| = −⌊x⌋ = −⌈x⌉ + C(x⊥1) \\ ⌊−x⌉_p \| = ⌈x⌋_p = −⌊x⌉_p + pC(x⊥p) \\ ⌈−x⌋_p \| = ⌊x⌉_p = −⌈x⌋_p + pC(x⊥p) \\ ⌊x + y⌋ \| = ⌊x⌋ + ⌊y⌋ + C(⌊x⌉_1 + ⌊y⌉_1 ≥ 1) \\ ⌊x + y⌉_p \| = ⌊x⌉_p + ⌊y⌉_p − pC(⌊x⌉_p + ⌊y⌉_p ≥ p) \\ ⌊x − y⌋ \| = ⌊x⌋ − ⌊y⌋ − C(⌊x⌉_1 \lt ⌊y⌉_1) \\ ⌊x − y⌉_p \| = ⌊x⌉_p − ⌊y⌉_p + pC(⌊x⌉_p \lt ⌊y⌉_p) \\ ⌈x + y⌉ \| = ⌈x⌉ + ⌈y⌉ − C(⌈x⌋_1 + ⌈y⌋_1 ≥ 1) \\ ⌈x + y⌋_p \| = ⌈x⌋_p + ⌈y⌋_p − pC(⌈x⌋_p + ⌈y⌋_p ≥ p) \\ ⌈x − y⌉ \| = ⌈x⌉ − ⌈y⌉ + C(⌈x⌋_1 \lt ⌈y⌋_1) \\ ⌈x − y⌋_p \| = ⌈x⌋_p − ⌈y⌋_p + pC(⌈x⌋_p \lt ⌈y⌋_p) \end{align}

 $${x}$$ $${⌊x⌉_3}$$ $${⌈x⌋_3}$$ −4 2 1 −3 0 0 −2 1 2 −1 2 1 0 0 0 1 1 2 2 2 1 3 0 0 4 1 2

### 12.2. Large Intermediate Results

Many of the calendar calculations that are described below are of the type

\begin{align} y \| = \floorratio{fx + t}{d} \label{eq:template} \\ e \| = (fx + t) \bmod d \label{eq:template-e} \end{align}

i.e.,

$\{y, e\} = \Div(fx + t, d)$

with

\begin{equation} fx + t = dy + e \end{equation}

where $$f$$, $$t$$, $$d$$ are fixed whole numbers with $$f, d \gt 1$$ and $$0 ≤ e \lt d$$ and $$y$$, $$x$$ are variable but whole numbers. The intermediate result $$fx + t$$ is then usually much greater in magnitude than $$x$$ and than the end result. This can give trouble on calculators and in computer programs where whole numbers cannot exceed a certain size that we'll refer to as $$w$$. (Floating-point numbers like 1.234 × 1020 can be much greater, but have limited accuracy, and then you have to worry about round-off errors.)

Intermediate results greater than $$w$$ give trouble, so to avoid trouble we'd need roughly $$|fx| ≤ w$$, so $$|x| ≤ \frac{w}{f}$$, which is much less than $$w$$ itself.

If $$|t| ≥ d$$, then we can rewrite equation \eqref{eq:template}ff to

\begin{align} fx + t \| = fx + \floorratio{t}{d} d + (t \bmod d) \\ y \| = \floorratio{t}{d} + \floorratio{fx + (t \bmod d)}{d} \\ e \| = (fx + (t \bmod d)) \bmod d \end{align}

where the second term of the last two equations has the same form as equation \eqref{eq:template}, if you rename $$t \bmod d$$ to a new $$t$$. Below, we assume that that has been done, which means that $$|t| \lt d$$.

If $$f \gt d$$, then we can rewrite equation \eqref{eq:template}ff to

\begin{align} fx + t \| = \left( \floorratio{f}{d} d + (f \bmod d) \right) x + t \\ y \| = \floorratio{f}{d} x + \floorratio{(f \bmod d) x + t}{d} \label{eq:template2} \\ e \| = ((f \bmod d)x + t) \bmod d \end{align}

Then $$|x|$$ may be nearly as great as $$\frac{w}{f \bmod d}$$ before we run into trouble.

Equations \eqref{eq:template2}ff again have the form of equations \eqref{eq:template}ff, if you rename $$f \bmod d$$ to a new $$f$$. Below, we assume that this has been done, so that $$f \lt d$$.

If we could first do the division by $$d$$ and then the multiplication by $$f$$, then the intermediate results would stay smaller than $$x$$ itself. We'd like to calculate something like $$f\floorratio{x}{d}$$ which is roughly equal to $$y$$, and then apply a correction to get the real $$y$$.

We can rewrite equation \eqref{eq:template}ff to

\begin{align} fx + t \| = f\left( \floorratio{x}{d} d + (x \bmod d) \right) + t \\ y \| = f\floorratio{x}{d} + \floorratio{f(x \bmod d) + t}{d} \label{eq:detour1} \\ e \| = (f(x \bmod d) + t) \bmod d \end{align}

Now the greatest intermediate result is no longer $$fx + t$$, which can get as large as $$fw$$, but $$f(x \bmod d) + t$$, which cannot exceed $$fd$$.

Equations \eqref{eq:detour1}ff again have the form of equations \eqref{eq:template}ff, if you rename $$x \bmod d$$ to a new $$x$$.

For example, let $$d = 12345$$, $$f = 8432$$, $$t = 871$$, and $$w = 2^{31} − 1 = 2,147,483,647$$, and suppose we want to calculate $$y$$, $$e$$ for $$x = 300,000$$.

We use the detour of equation \eqref{eq:detour1}. We find

\begin{align*} \floorratio{x}{d} \| = \floorratio{300000}{12345} = 24 \\ x \bmod d \| = 300000 \bmod 12345 = 3720 \\ y = \| f\floorratio{x}{d} + \floorratio{f(x \bmod d) + t}{d} \\ \| = 8432×24 + \floorratio{8432×3720 + 871}{12345} \\ \| = 202368 + \floorratio{31367911}{12345} \\ \| = 202368 + 2540 = 204908 \\ e \| = 31367911 \bmod 12345 = 11611 \end{align*}

The greatest intermediate result is 31,367,911, which is much less than $$w$$.

If we could have used arbitrarily large intermediate results, then we'd have found

\begin{align*} y \| = \floorratio{fx + t}{d} = \floorratio{8432×300000 + 871}{12345} = \floorratio{2529600871}{12345} = 204908 \\ e \| = 2529600871 \bmod 12345 = 11611 \end{align*}

which are the same results, but with a greatest intermediate result of 2,529,600,871 which is greater than $$w$$.

Sometimes an intermediate result of (nearly) $$fd$$ is still too great, because $$fd$$ can be much greater than $$w$$ even if $$d$$ and $$f$$ are each much less than $$w$$. If $$fd$$ must remain less than $$w$$, then $$d$$ and $$f$$ cannot both be greater than $$\sqrt{w}$$, which is a lot less than $$w$$ itself. For example, for 32-bit numbers, $$w = 2^{31} − 1 = 2,147,483,647$$ and $$\sqrt{w} = 46341$$, so even if $$d$$ and $$f$$ are both just a bit greater than 46431, which isn't really very large, then $$fd$$ is already too great to fit into a 32-bit number.

Suppose we pick a $$P \gt 0$$ and then calculate once

\begin{align} \{Q, R\} \| = \Div(fP, d) \\ Q \| = \floorratio{fP}{d} \\ R \| = fP \bmod d \end{align}

so

\begin{equation} fP = dQ + R \end{equation}

Then, for each desired $$x$$, calculate

\begin{align} \{q, r\} \| = \Div(x, P) \\ q \| = \floorratio{x}{P} \\ r \| = x \bmod P \end{align}

so

\begin{equation} x = qP + r \end{equation}

and then

\begin{align} fx + t \| = fqP + fr + t = qdQ + qR + fr + t \\ y \| = \floorratio{qdQ + qR + fr + t}{d} = qQ + \floorratio{qR + fr + t}{d} \label{eq:detour2} \\ e \| = (qdQ + qR + fr + t) \bmod d = (qR + fr + t) \bmod d \end{align}

Now the greatest intermediate result is $$m ≡ qR + fr + t$$. We know that $$r \lt P$$, so $$fr \lt fP$$. To stay out of trouble we need $$|m| ≤ w$$. We have

$|m| ≤ \left| \floorratio{x}{P} \right| R + fP + |t| ≤ |x|\frac{R}{P} + fP + |t|$

so we need

$|x|\frac{R}{P} + fP + |t| ≤ w$

If $$R = 0$$, then we need $$fP + |t| ≤ w$$, so then

\begin{equation} P ≤ \frac{w − |t|}{f} \end{equation}

If $$R \gt 0$$ then we need

\begin{equation} |x| ≤ X ≡ (w − fP − |t|) \frac{P}{R} \end{equation}

We must have $$0 \lt P \lt \frac{w − |t|}{f} ≤ \frac{w}{f}$$, otherwise certainly $$X ≤ 0$$.

We want to make the range of $$x$$ as large as possible, so we seek $$P$$ and $$R$$ such that $$X$$ becomes as great as possible, given $$w$$, $$f$$, $$t$$. This means that $$R$$ should preferably be small.

If we use equations \eqref{eq:detour1}ff then it is sufficient if $$X ≥ d$$ to be able to handle all values $$|x| \lt w$$, because then we can use equations \eqref{eq:detour1}ff to first make $$|x| \lt d$$. If we do not use equations \eqref{eq:detour1}ff, then we must have $$X ≥ w$$ to be able to handle all values $$|x| \lt w$$.

There can be multiple combinations of $$P$$, $$Q$$, $$R$$ that yield $$X ≥ w$$ or $$X ≥ d$$, and those are all good enough. Within that group, we prefer small $$P$$, $$Q$$, $$R$$, because that makes for easier calculations.

The smallest possible positive value of $$R$$ is equal to the greatest common divisor $$c$$ of $$d$$ and $$f$$, and the corresponding $$P, Q \gt 0$$ can be found using the Extended Algorithm of Euclid ― or by trying all $$d$$ or $$w/f$$ (pick the smallest) possibilities for $$P$$.

The Extended Algorithm of Euclid produces a relation of the form

\begin{equation} φf − δd = c \end{equation}

for $$c \gt 0$$ (if you find $$c \lt 0$$, then multiply $$φ$$, $$δ$$, and $$c$$ by −1). If $$φ$$ and $$δ$$ are positive, then $$P_1 = φ$$ and $$Q_1 = δ$$, but $$φ$$ and $$δ$$ can be negative. In general, calculate $$P_1$$, $$Q_1$$, $$R_1$$ as follows:

\begin{align*} P_1 \| = φ \bmod \frac{d}{c} \\ Q_1 \| = δ \bmod \frac{f}{c} \\ R_1 \| = c \end{align*}

If that $$P = P_1$$, $$Q = Q_1$$, $$R = R_1$$ fit together, then

\begin{equation} fP_1 − dQ_1 = R_1 \end{equation}

Then also $$P = kP_1$$, $$Q = kQ_1$$, $$R = kR_1$$ belong together, for whole $$0 \lt k ≤ \floorratio{d}{R_1}$$, because $$R$$ must be less than $$d$$. To be able to handle greater values of $$k$$, it is therefore better to calculate $$Q$$ and $$R$$ from $$P$$ according to their definition.

\begin{align*} P \| = kP_1 \\ Q \| = \floorratio{fP}{d} = \floorratio{kfP_1}{d} = \floorratio{k(dQ_1 + R_1)}{d} = kQ_1 + \floorratio{kR_1}{d} \\ R \| = fP \bmod d = k(dQ_1 + R_1) \bmod d = kR_1 \bmod d \end{align*}

so the curtailing of $$R$$ to values less than $$d$$ leads to an increase in $$Q$$ so that $$fP − dQ = R$$ is still satisfied. If $$kR_1 \lt d$$, then $$P = kP_1$$, $$Q = kQ_1$$, and $$R = kR_1$$ as expected.

It is not useful to allow $$P ≥ d$$, because otherwise the greatest intermediate result can be as great or greater than $$fP = fd$$ which can become greater than $$w$$, because if it couldn't exceed $$w$$ then we could have used equation \eqref{eq:detour1} and would not need the current more complicated method at all. We seek $$P \lt d$$.

If $$P_2 = P + md/c$$, with $$c$$ the greatest common divisor of $$f$$ and $$d$$, then

\begin{align*} Q_2 \| = \floorratio{fP_2}{d} = \floorratio{fP + m\dfrac{fd}{c}}{d} = \floorratio{fP}{d} + m\frac{f}{c} = Q + m\dfrac{f}{c} \\ R_2 \| = fP_2 − dQ_2 = fP + m\dfrac{fd}{c} − \left( dQ + m \dfrac{df}{c} \right) = fP − dQ = R \end{align*}

so if we add or subtract a multiple of $$d/c$$ from $$P$$, then $$Q$$ shifts by the corresponding multiple of $$f/c$$, and $$R$$ remains the same. In particular, we can take

\begin{align} P \| = kP_1 \bmod d \\ Q \| = kQ_1 \bmod f \\ R \| = kR_1 \bmod d \end{align}

for $$k \lt d$$ because there are not more than that number of unique values of $$P \lt d$$.

For example, let $$d = 765433$$, $$f = 25920$$, $$t = 13835$$, $$w = 2^{31} − 1 = 2147483647$$. Then $$\frac{w}{f} = \frac{2^{31} − 1 }{25920} ≈ 82850$$, so we seek a $$P \lt 82850 \lt 765433$$, and an $$R$$ as small as possible.

The Extended Algorithm of Euclid (or a search) for this $$d$$ and $$f$$ yields that $$99902f − 3383d = 1$$, so $$P_1 = 99902$$, $$Q_1 = 3383$$, $$R_1 = 1$$. Then

\begin{align*} P \| = kP_1 \bmod d \\ Q \| = kQ_1 \bmod f \\ R \| = kR_1 \bmod d \end{align*}

The following table shows a couple of combinations for small $$R$$.

$${R}$$ $${P}$$ $${Q}$$ $${Q′}$$
1 99902 3383 3383
2 199804 6766 6766
7 699314 23681 23681
8 33783 1144 1144
9 133685 4527 4527

The next table shows the combinations with the greatest $$X$$.

$${R}$$ $${P}$$ $${Q}$$ $${X}$$
8 33783 1144 5.4 × 1012
16 67566 2288 1.7 × 1012
31 35230 1193 1.4 × 1012
54 36677 1242 8.1 × 1011
39 69013 2337 6.3 × 1011

All of these $$X$$ are comfortably greater than $$w ≈ 2.15×10^{9}$$. $$X ≥ w$$ for 1498 values of $$R$$ between 8 and 20312, and $$X ≥ d$$ for 82576 values of $$R$$ between 8 and 765426. We found solutions for $$X ≥ w$$ so we do not need equations \eqref{eq:detour1}ff. Within the group with $$X ≥ w$$ we search for the smallest values of $$PQR$$. We find

$${R}$$ $${P}$$ $${Q}$$ $${PQR}$$ $${X}$$
23 1447 49 1.6 × 106 1.33 × 1011
46 2894 98 1.3 × 107 1.30 × 1011
544 945 32 1.6 × 107 3.69 × 109
69 4341 147 4.4 × 107 1.28 × 1011
92 5788 196 1.0 × 108 1.26 × 1011

We choose $$P = 1447$$, then $$R = 23$$ and $$Q = 49$$.

Now we calculate $$y$$, $$e$$ for $$x = 1710321$$. We find

\begin{align*} q \| = \floorratio{x}{P} = \floorratio{1710321}{1447} = 1181 \\ r \| = x \bmod P = 1710321 \bmod 1447 = 1414 \\ y \| = qQ + \floorratio{qR + fr + t}{d} \\ \| = 1181×49 + \floorratio{1181×23 + 25920×1414 + 13835}{765433} \\ \| = 57869 + \floorratio{36691878}{765433} \\ \| = 57869 + 47 = 57916 \\ e \| = 36691878 \bmod 765433 = 716527 \end{align*}

The greatest intermediate result is 36,691,878, which is considerably less than $$w$$.

Had we been allowed arbitrarily large intermediate results, then we'd have found

\begin{align*} y \| = \floorratio{fx + t}{d} = \floorratio{25920×1710321 + 13835}{765433} = \floorratio{44331534155}{765433} = 57916 \\ e \| = 44331534155 \bmod 765433 = 716527 \end{align*}

which are the same results, but with the much greater intermediate result of 44,331,534,155.

Another example: $$d = 146097$$, $$f = 4800$$, $$t = 15793$$, $$w = 2^{31} − 1 = 2147483647$$. Then $$\frac{w}{f} = \frac{2^{31} − 1}{4800} ≈ 447392$$, so we seek a $$P \lt 146097 \lt 447392$$ and an $$R$$ as small as possible.

The Extended Algorithm of Euclid (or a search) yields that $$−15188f + 499d = 3$$, and there is no sum of a multiple of $$f$$ and a multiple of $$d$$ that is closer to 0. It follows that

\begin{align*} φ \| = −15188 \\ δ \| = −499 \\ c \| = 3 \\ P_1 \| = −15188 \bmod \frac{146097}{3} = −15188 \bmod 48699 = 33511 \\ Q_1 \| = −499 − \floorratio{−15188×3}{146097} \frac{4800}{3} = −499 − (−1)×1200 = 1101 \\ R_1 \| = 3 \end{align*}

Let's check:

$fP_1 − dQ_1 = 4800×33511 − 146097×1101 = 3 = R_1$

For small $$R$$ we find the following combinations:

$${k}$$ $${R}$$ $${P}$$ $${Q}$$
1 3 33511 1101
2 6 67022 2202
3 9 100533 3303
4 12 134044 4404

There are 57132 combinations for which $$X ≥ w$$. The combinations with the smallest $$PQR$$ are:

$${R}$$ $${P}$$ $${Q}$$ $${PQR}$$ $${X}$$
48 487 16 3.74 × 105 2.18 × 1010
9 3135 103 2.91 × 106 7.43 × 1011
96 974 32 2.99 × 106 2.17 × 1010
375 761 25 7.13 × 106 4.35 × 109
144 1461 48 1.01 × 107 2.17 × 1010

We choose $$P = 487$$, then $$Q = 16$$ and $$R = 48$$. We calculate $$y$$, $$e$$ for $$x = 731767$$. Then

\begin{align*} q \| = \floorratio{731767}{487} = 1502 \\ r \| = 731767 \bmod 487 = 293 \\ y \| = 1502×16 + \floorratio{1502×48 + 4800×293 + 15793}{146097} \\ \| = 24032 + \floorratio{1494289}{146097} = 24032 + 10 = 24042 \\ e \| = 1494289 \bmod 146097 = 33319 \end{align*}

The greatest intermediate result is 1.494.289, which is comfortably smaller than $$w$$. With the original formula we'd have found

\begin{align*} y \| = \floorratio{4800×731767 + 15793}{146097} = \floorratio{3512497393}{146097} = 24042 \\ e \| = 3512497393 \bmod 146097 = 33319 \end{align*}

which are the same results as before but with the greatest intermediate result equal to 3,512,497,393, which is considerably larger than $$w$$.

I've investigated for all $$w$$ from 3 through 1024 what the greatest values of $$f$$ and $$d$$ are for which solutions can be found with $$X ≥ d$$ or $$X ≥ w$$, and what the greatest $$P$$, $$Q$$, $$R$$, and $$X$$ are among those solutions, and what the greatest number $$c$$ of such solutions is, and the total count $$N$$ of combinations of $$f$$ and $$d$$ for which there is at least one solution. I find

\begin{align} \max(f_w) \| ≈ \floorratio{w}{4} \\ \max(f_d) \| ≈ \floorratio{w}{3} \\ \max(d_w) \| ≈ w − 5 + (w \bmod 2) \\ \max(d_d) \| ≈ w − 1 \\ \max(c_w) \| ≈ \floorratio{w+8}{16} \\ \max(c_d) \| ≈ \floorratio{w−1}{2} \\ \max(P_w) \| ≈ \floorratio{w}{2} − 2 + (w \bmod 2) \\ \max(P_d) \| ≈ \floorratio{w}{2} − 1 \\ \max(Q) \| ≈ \floorratio{2\sqrt{w − 1} − 3}{2} \\ \max(R_w) \| ≈ \floorratio{\sqrt{w + 1} − 1}{2} \\ \max(R_d) \| ≈ \frac{\sqrt{w(w + 1100)}}{25} \\ \max(X) \| ≈ \floorratio{w^2}{8} \\ N_w \| ≈ \frac{1}{5}w^{5/3} \\ N_d \| ≈ \frac{1}{3}w^{5/3} \end{align}

where the variables with subscript $$w$$ apply only to $$X \gt w$$, those with subscript $$d$$ apply only to $$X \gt d$$, and those without a subscript apply to both. The formulas for $$\max(R_d)$$, $$N_w$$, and $$N_d$$ are approximations; the other formulas are exact for $$10 ≤ w ≤ 1024$$.

For example, for $$w = 1023$$ I find

\begin{align*} \max(f_w) \| ≈ \floorratio{1023}{4} = 255 \\ \max(f_d) \| ≈ \floorratio{1023}{3} = 341 \\ \max(d_w) \| ≈ 1023 − 5 + (1023 \bmod{2}) = 1019 \\ \max(d_d) \| ≈ 1023 − 1 = 1022 \\ \max(c_w) \| ≈ \floorratio{1023 + 8}{16} = 64 \\ \max(c_d) \| ≈ \floorratio{1023 − 1}{2} = 511 \\ \max(P_w) \| ≈ \floorratio{1023}{2} − 2 + (1023 \bmod{2}) = 510 \\ \max(P_d) \| ≈ \floorratio{1023}{2} − 1 = 510 \\ \max(Q) \| ≈ \floorratio{2×\sqrt{1023 − 1} − 3}{2} ≈ ⌊30.47⌋ = 30 \\ \max(R_w) \| ≈ \floorratio{\sqrt{1023 + 1} − 1}{2} ≈ ⌊15.50⌋ = 15 \\ \max(R_d) \| ≈ \frac{\sqrt{1023 × (1023 + 1100)}}{25} ≈ 58.95 \\ \max(X) \| ≈ \floorratio{1023^2}{8} = 130816 \\ N_w \| ≈ \frac{1}{5}1023^{5/3} ≈ 20772.52 \\ N_d \| ≈ \frac{1}{3}1023^{5/3} ≈ 34620.87 \end{align*}

These values are all correct, except that $$R_d = 59$$, $$N_w = 20014$$, and $$N_d = 33937$$.

If we extrapolate the above formules to $$w = 2^{31} − 1$$ (for values with a width of 32 bits), then we find, approximately

\begin{align*} \max(f_w) \| ≈ 5.4×10^8 \\ \max(f_d) \| ≈ 7.2×10^8 \\ \max(d) \| ≈ 2.1×10^9 \\ \max(c_w) \| ≈ 1.3×10^8 \\ \max(c_d) \| ≈ 1.1×10^9 \\ \max(P) \| ≈ 1.1×10^9 \\ \max(Q) \| ≈ 46340 \\ \max(R_w) \| ≈ 23170 \\ \max(R_d) \| ≈ 8.6×10^7 \\ \max(X) \| ≈ 5.8×10^{17} \\ N_w \| ≈ 7.1×10^{14} \\ N_d \| ≈ 1.2×10^{15} \end{align*}

so if we seek a solution for $$X ≥ w$$ (so that we do not need equations \eqref{eq:detour1}ff), then we need try only up to 23170 different values of $$R$$. If such a solution does not exist, then we seek a solution for $$X ≥ d$$ (for which we do need to use equations \eqref{eq:detour1}) and then we must try up to 86 million values of $$R$$, which is still much less than the up to 1100 million values of $$P$$.

For a given $$w$$ there are many combinations of $$d$$ and $$f$$ for which there are no solutions with $$X ≥ w$$ or $$X ≥ d$$. For example, for $$w = 499$$ there are only 6554 combinations of $$d$$ and $$f$$ with at least one solution with $$X ≥ w$$, and 10560 combinations with at least one solution with $$X ≥ d$$, but there are $$\frac{1}{2} (w − 2)×(w − 3) = 123256$$ combinations with $$2 ≤ f \lt d \lt w$$. The greater $$f$$ is, the smaller is the chance that there is a solution for the combination of that $$f$$ and an arbitrary $$d \gt f$$.

However, when $$f$$ and $$d$$ are considerably less than $$w$$, then there is a good chance of a solution. For example, for $$w = 2^{31} − 1$$ and for $$f$$ and $$d$$ near $$f ≈ 25920$$ and $$d ≈ 765433$$ there is no solution with $$X ≥ d$$ for only 1 out of about 9100 combinations of $$f$$ and $$d$$, and no solution with $$X ≥ w$$ for only 1 out of about 54 combinations.

### 12.3. The Simple Calendar

For convenience, we'll work with a calendar that has only "days" and "months", where the months are longer than the days, but the formulas also work for other units of time. Later, we'll discuss calendars with more than two time periods.

In the simplest case, the average length of the month is equal to $$p$$ days, and all months are either $$⌊p⌋$$ or $$⌊p⌋ + 1$$ days long. The running day number $$y$$ depends on the month number $$x$$ and the day number $$z$$ within the current month as follows:

\begin{align} c(x) \| ≡ ⌊px + r⌋ \\ y \| ≡ c(x) + z \end{align}

where $$x$$, $$y$$, $$c(x)$$, $$z$$ are whole numbers, and $$p ≥ 1$$ and $$r$$ are fixed values that depend on the calendar. The first month has $$x = 0$$ and the first day of the month has $$z = 0$$, for easier calculation.

The first day ($$z = 0$$) of the first month ($$x = 0$$) has running day number $$y = 0$$, so

\begin{equation} ⌊r⌋ = 0 ⇔ 0 ≤ r \lt 1 \end{equation}

The length $$L(x)$$ of month $$x$$ is

\begin{equation} \begin{split} L(x) \| = c(x + 1) − c(x) \\ \| = ⌊px + r + p⌋ − ⌊px + r⌋ \\ \| = ⌊px + r⌋ + ⌊p⌋ + C(⌊px + r⌉_1 + ⌊p⌉_1 ≥ 1) − ⌊px + r⌋ \\ \| = ⌊p⌋ + C(⌊px + r⌉_1 ≥ 1 − ⌊p⌉_1) \end{split} \end{equation}

All months are $$⌊p⌋$$ or $$⌊p⌋ + 1$$ days long. Month $$x$$ is a long month (with $$⌊p⌋ + 1$$ days) if $$⌊px + r⌉_1 ≥ 1 − ⌊p⌉_1$$. On average there is a long month after every

\begin{equation} Q = \frac{1}{⌊p⌉_1} \label{eq:Q} \end{equation}

months, but that isn't always a whole number, so in practice the number of months between two successive long months is equal to $$⌊Q⌋$$ or $$⌈Q⌉$$.

With $$r = 0$$ we get a certain pattern of long and short months. Suppose that that pattern would be good if only it were shifted by $$a$$ months so that month $$x_0$$ in the calendar with $$r = 0$$ corresponded with month $$x = x_0 + a$$ in the new calendar. Then

\begin{equation} \begin{split} c(x) \| = c(x_0 + a) − c(a) \\ \| = ⌊p(x_0 + a)⌋ − ⌊pa⌋ \\ \| = ⌊px_0 + pa − ⌊pa⌋⌋ \\ \| = ⌊px_0 + ⌊pa⌉_1⌋ \end{split} \end{equation}

so

\begin{equation} r = ⌊pa⌉_1 \end{equation}

The subtracting of $$c(a)$$ ensures that $$c(0) = 0$$.

Then we go from running month number $$x$$ and day number $$z$$ in the month to running day number $$y$$ through:

\begin{equation} y = ⌊px + r⌋ + z \label{eq:xnaary} \end{equation}

How do we go from $$y$$ back to $$x$$?

\begin{eqnarray} y = ⌊px + r⌋ + z \| = \| px + r − ⌊px + r⌉_1 + z \\ x + \frac{z − ⌊px + r⌉_1}{p} \| = \| \frac{y − r}{p} \\ x + \frac{z + 1 − ⌊px + r⌉_1}{p} \| = \| \frac{y + 1 − r}{p} \end{eqnarray}

We have

\begin{equation} 0 \lt 1 − ⌊px + r⌉_1 ≤ 1 \end{equation}

for all $$x$$, so if $$z ≥ 0$$ then

\begin{equation} z + 1 − ⌊px + r⌉_1 \gt 0 \end{equation}

For the last day of month $$x$$ we have

\begin{equation} z = L(x) − 1 = ⌊px + r + p⌋ − ⌊px + r⌋ \end{equation}

so if $$z ≤ L(x) − 1$$ then

\begin{equation} \begin{split} z + 1 − ⌊px + r⌉_1 \| ≤ ⌊px + r + p⌋ − ⌊px + r⌋ − ⌊px + r⌉_1 \\ \| = ⌊px + r + p⌋ − px − r \\ \| = px + r + p − ⌊px + r + p⌉_1 − px − r \\ \| = p − ⌊px + r + p⌉_1 ≤ p \end{split} \end{equation}

Combined, this means, for $$0 ≤ z ≤ L(x) − 1$$,

\begin{eqnarray} 0 \| \lt \| z + 1 − ⌊px + r⌉_1 ≤ p \\ 0 \| \lt \| \dfrac{z + 1 − ⌊px + r⌉_1}{p} ≤ 1 \end{eqnarray}

so

\begin{eqnarray} x + 1 \| = \| \left\lceil x + \frac{z + 1 − ⌊px + r⌉_1}{p} \right\rceil = \ceilratio{y + 1 − r}{p} \\ x \| = \| \ceilratio{y + 1 − r}{p} − 1 \label{eq:ξ} \end{eqnarray}

And then we can also find $$z$$

\begin{equation} \begin{split} z \| = y − ⌊px + r⌋ \\ \| = y − \left\lfloor p \left( \ceilratio{y + 1 − r}{p} − 1 \right) + r \right\rfloor \\ \| = y − \left\lfloor p \left( \dfrac{y + 1 − r}{p} + \left\lceil \dfrac{y + 1 − r}{p} \right\rfloor_1 − 1 \right) + r \right\rfloor \\ \| = y − \left\lfloor y + 1 − r + p \left\lceil \dfrac{y + 1 − r}{p} \right\rfloor_1 − p + r \right\rfloor \\ \| = −\left\lfloor 1 + ⌈y + 1 − r⌋_p − p \right\rfloor \\ \| = ⌈p − 1 − ⌈y + 1 − r⌋_p⌉ \\ \| = ⌈p − ⌈y + 1 − r⌋_p⌉ − 1 \end{split} \end{equation}

So, all together,

\begin{eqnarray} x \| = \| \ceilratio{y + 1 − r}{p} − 1 \label{eq:ynaarx2} \\ z \| = \| ⌈p − ⌈y + 1 − r⌋_p⌉ − 1 \end{eqnarray}

We can rewrite this so we can use the $$\Div$$ function. If

\begin{eqnarray} k \| = \| y + 1 − r \\ \{ m,n \} \| = \| \Div(−k, p) \end{eqnarray}

then

\begin{eqnarray} x \| = \| −m − 1 \\ z \| = \| −⌊n − p⌋ − 1 \end{eqnarray}

An example, with $$p = 30.6$$ and $$r = 0$$. We then find, for the beginning of the first few months

 $${x}$$ 0 1 2 3 4 5 6 7 8 9 10 $${px}$$ 0 30.6 61.2 91.8 122.4 153 183.6 214.2 244.8 275.4 306 $${c(x)}$$ 0 30 61 91 122 153 183 214 244 275 306 $${L(x)}$$ 0 30 31 30 31 31 30 31 30 31 31

Because $$5p = 153$$ is a whole number, the pattern of month lengths repeats itself after 5 months. We see here the pattern 31-30-31-30-31 that in the Gregorian calendar describes the months March - July and August - December.

Now we go in the opposite direction, for a few running day numbers:

 $${y}$$ −2 −1 0 1 120 121 122 123 $${k}$$ 1 0 −1 −2 −121 −122 −123 −124 $${m}$$ 0 0 −1 −1 −4 −4 −5 −5 $${n}$$ 1 0 29.6 28.6 1.4 0.4 30 29 $${x}$$ −1 −1 0 0 3 3 4 4 $${z}$$ 29 30 0 1 29 30 0 1

### 12.4. With Whole Numbers Only

Calculating machines often work with a limited number of decimals behind the decimal separator, so you can get into trouble with round-off errors. If $$(y + 1)/p = 6.99999999998$$ but because of a very small round-off error your calculating machine thinks that $$(y + 1)/p = 7.00000000001$$, then your calculating machine thinks that $$⌈(y + 1)/p⌉ − 1 = 7$$ rather than 6, and then you find the wrong month.

Converting equations with $$⌈•⌉$$ or $$⌈•⌋_1$$ into equations with $$⌊•⌋$$ or $$⌊•⌉_1$$ or $$\Div$$ is easier when only whole numbers are used, because for arbitrary whole numbers $$v$$ and $$w$$ ($$w \gt 0$$)

\begin{align} \ceilratio{v}{w} \| = \floorratio{v − 1}{w} + 1 \\ ⌈v⌋_w \| = w − 1 − ⌊v − 1⌉_w \end{align}

If the average length $$p$$ is a ratio (of whole numbers), then we can avoid such round-off errors by rewriting the formulas based on ratios. We define

\begin{equation} p = \frac{f}{g} \end{equation}

with $$f$$, $$g$$ whole numbers greater than zero. Then we find, for an arbitrary value $$q$$,

\begin{equation} \begin{split} ⌊q⌋_p \| = p\left\lfloor \dfrac{q}{p} \right\rceil_1 \| = \dfrac{f}{g} \left\lfloor \dfrac{qg}{f} \right\rceil_1 \\ \| = \dfrac{1}{g} ⌊qg⌉_f \end{split} \end{equation}

Then

\begin{eqnarray} r \| = \| ⌊pa⌉_1 = \left\lfloor \frac{fa}{g} \right\rceil_1 = \frac{⌊fa⌉_g}{g} ≡ \frac{s}{g} \label{eq:r} \\ x \| = \| \ceilratio{y + 1 − r}{p} − 1 \notag \\ \| = \| \ceilratio{y + 1 − (s/g)}{f/g} − 1 \notag \\ \| = \| \ceilratio{gy + g − s}{f} − 1 \notag \\ \| = \| \floorratio{gy + g − s − 1}{f} \\ z \| = \| \left\lceil \dfrac{f}{g} − ⌈y + 1 − (s/g)⌋_p \right\rceil − 1 \notag \\ \| = \| \ceilratio{f − ⌈gy + g − s⌋_f}{g} − 1 \notag \\ \| = \| \ceilratio{f − (f − 1 − ⌊gy + g − s − 1⌉_f)}{g} − 1 \notag \\ \| = \| \ceilratio{f − f + 1 + ⌊gy + g − s − 1⌉_f}{g} − 1 \notag \\ \| = \| \ceilratio{1 + ⌊gy + g − s − 1⌉_f}{g} − 1 \notag \\ \| = \| \floorratio{⌊gy + g − s − 1⌉_f}{g} \end{eqnarray}

Summarizing, from running month number $$x$$ and day number $$z$$ in the month to running day number $$y$$:

\begin{equation} y = \floorratio{fx + s}{g} + z \end{equation}

where

\begin{equation} s = fa \bmod g \end{equation}

and from $$y$$ to $$x$$ and $$z$$:

\begin{align} k \| = gy + g − 1 − s \\ x \| = \floorratio{k}{f} \label{eq:ynaarxr} \\ z \| = \floorratio{k \bmod f}{g} \end{align}

Now you can do all calculations with ratios, which have no numbers after the decimal mark and hence no round-off errors.

For the same calendar as before we have $$p = 30.6 = 153/5$$, so $$f = 153$$, $$g = 5$$. Also $$r = 0$$ so $$a = 0$$. We then find, for the beginning of the first few months

 $${x}$$ 0 1 2 3 4 5 6 7 8 9 10 $${⌊fx/g⌋}$$ 0 30 61 91 122 153 183 214 244 275 306 $${⌈fx⌋_g}$$ 0 3 1 4 2 0 3 1 4 2 0 $${c(x)}$$ 0 30 61 91 122 153 183 214 244 275 306 $${L(x)}$$ 0 30 31 30 31 31 30 31 30 31 31

Now we go in the opposite direction, for a few running day numbers:

 $${y}$$ −2 −1 0 1 120 121 122 123 $${k}$$ −6 −1 4 9 604 609 614 619 $${x}$$ −1 −1 0 0 3 3 4 4 $${n}$$ 147 152 4 9 145 150 2 7 $${z}$$ 29 30 0 1 29 30 0 1

### 12.5. Very Unequal Months

So far we have assumed that a long month was only one day longer than a short month, but that difference can be larger. Suppose that short months are $$q$$ days long and that long months are $$q + d$$ days long, with $$q$$, $$d$$ whole numbers greater than 0, and that a fraction $$ψ$$ (between 0 and 1) of all months is long. Then the average length of a month is equal to

\begin{equation} p = q + dψ \end{equation}

days. The formula to calculate the running day number $$y$$ from month number $$x$$ and day number $$z$$ in the current month is then

\begin{equation} \begin{split} y \| = c + z = qx + d⌊ψ(x + a)⌋ − d⌊ψa⌋ + z \\ \| = qx + d⌊ψx + ψa − ⌊ψa⌋⌋ + z = qx + d⌊ψx + σ⌋ + z \end{split} \label{eq:xnaary3} \end{equation}

\begin{equation} σ ≡ ψa − ⌊ψa⌋ = ψa \bmod 1 \end{equation}

\begin{equation} c = qx + d⌊ψx + σ⌋ \end{equation}

How do we go in the opposite direction? To figure that out we compare the running day number $$c$$ that comes from equation \eqref{eq:xnaary3} for the first day ($$z = 0$$) of month $$x$$ with the running day number (for convenience renamed to $$c_s$$) that comes from equation \eqref{eq:xnaary} for the same $$x$$ and the same average month length $$p$$ (but with $$d = 1$$ and $$a = 0$$).

\begin{equation} \begin{split} c_s(x) \| ≡ ⌊px⌋ \\ \| = ⌊qx + dψx⌋ \\ \| = qx + ⌊dψx⌋ \end{split} \end{equation}

Then we find

\begin{equation} c − c_p = d⌊ψx + σ⌋ − ⌊dψx⌋ = d(ψx + σ − δ) − (dψx − ε) = dσ − dδ + ε \end{equation}

for certain $$δ$$, $$ε$$ satisfying

\begin{equation} 0 ≤ \{δ,ε\} \lt 1 \end{equation}

so

\begin{equation} dσ − d \lt c − c_p \lt dσ + 1 \end{equation}

\begin{equation} dσ − d + 1 ≤ c − c_p ≤ dσ \end{equation}

so the beginning of month $$x$$ in the target calendar is between $$dσ + 1 − d$$ and $$dσ$$ days after the beginning of month $$x$$ in the simpler calendar. Because $$0 ≤ σ \lt 1$$, we also have $$−d \lt c − c_p \lt d$$, so the difference between the beginning of a particular month in the target calendar and the same month in the simpler calendar is always less than $$d$$ days (in either direction).

For that simpler calendar we have formula \eqref{eq:ynaarx2} to calculate the corresponding month number for any of the $$\lfloor p\rfloor$$ or $$⌈p⌉$$ days in that month, so if running day number $$y$$ corresponds to one of the first $$\lfloor p\rfloor$$ days of month $$x$$ in the target calendar, then we'll have for that $$x$$

\begin{align} x_\text{lower} \| ≤ x ≤ x_\text{upper} \label{eq:ondbov3} \\ x_\text{lower} \| ≡ \ceilratio{y − dσ + 1}{p} − 1 \\ x_\text{upper} \| ≡ \ceilratio{y − dσ + d}{p} − 1 \label{eq:ynaarx3} \end{align}

In the target calendar, a month can be up to $$q + d$$ days long, while in the corresponding simpler calendar a month cannot be longer than $$⌈p⌉$$ days, which is less than $$q + d$$. A number (less than $$d$$) of days beyond day $$\lfloor p \rfloor$$ of a month can still belong to that same month in the target calendar, but already belong to a later month in the simpler calendar. For such days, formula \eqref{eq:ynaarx3} can yield a month number that is too great, but then formula \eqref{eq:ondbov3} still holds.

If $$d ≤ p$$ then the difference between $$x_\text{upper}$$ and $$x_\text{lower}$$ is at most 1. In that case

\begin{align} c_\text{upper} \| = qx_\text{upper} + d⌊ψx_\text{upper} + σ⌋ \\ z_\text{upper} \| = y − c_\text{upper} \\ ζ \| = \floorratio{z_\text{upper}}{q + d} \\ x \| = x_\text{upper} + ζ \label{eq:xζ3} \\ z \| = y − c = y − qx − d⌊ψx + σ⌋ \end{align}

If $$f = 1001$$, $$g = 165$$, $$d = 2$$, $$q = 5$$, $$a = 77$$, then

\begin{align*} p \| = \frac{f}{g} = \frac{1001}{165} = 6 \frac{11}{165} \\ ψ \| = \frac{176}{330} \\ σ \| = ψa \bmod 1 = \frac{13552}{330} \bmod 1 = \frac{22}{330} \end{align*}

Then also $$d ≤ p$$, so equation \eqref{eq:xζ3} can be used.

The running day number $$y$$ of the first day of month number $$x = 37$$ is then (from equation \eqref{eq:xnaary3})

$y = qx + d⌊ψx + σ⌋ = 5×37 + 2×\left\lfloor \frac{37×176}{330} + \frac{22}{330} \right\rfloor = 185 + 2×\floorratio{6534}{330} = 223$

Now we calculate the month that corresponds to running day number $$y = 223$$. Then

\begin{align*} x_\text{lower} \| = \ceilratio{y + 1}{p} − 1 = \ceilratio{224×165}{1001} − 1 = 36 \\ x_\text{upper} \| = \ceilratio{y + d}{p} − 1 = \ceilratio{225×165}{1001} − 1 = 37 \end{align*}

so the month number can be 36 or 37. For $$x_\text{upper}$$ we find

\begin{align*} c_\text{upper} \| = 5×37 + 2×\floorratio{176×37}{330} + \frac{22}{330} = 223 \\ z_\text{upper} \| = y − c_\text{upper} = 223 − 223 = 0 \\ ζ \| = 0 \\ x \| = x_\text{upper} = 37 \\ z \| = z_\text{upper} = 0 \end{align*}

And here is another example. With $$f = 829$$, $$g = 235$$, $$d = 2$$, $$q = 3$$, $$ψ = 124/470$$, $$a = 0$$ we calculate which month corresponds to running day number $$y = 1448$$. Then

\begin{align*} x_\text{lower} \| = \ceilratio{y + 1}{p} − 1 = \ceilratio{1449×235}{829} − 1 = 410 \\ x_\text{upper} \| = \ceilratio{y + d}{p} − 1 = \ceilratio{1450×235}{829} − 1 = 411 \end{align*}

so the month number can be 410 or 411. For $$x_\text{upper}$$ we find

\begin{align*} c_\text{upper} \| = 3×411 + 2×\floorratio{411×124}{470} = 1449 \\ z_\text{upper} \| = y − c_\text{upper} = 1448 − 1449 = −1 \\ ζ \| = −1 \\ x \| = x_\text{upper} + ζ = 410 \\ z \| = y − c = 1448 − 3×410 − 2×\floorratio{411×124}{470} = 2 \end{align*}

If $$p = f/g = q + dh/g$$ is a ratio of whole numbers ($$f$$, $$g$$, $$q$$, $$d$$, $$h$$ are whole numbers), then we find

\begin{align} y \| = c + z = qx + d\floorratio{hx + s}{g} + z \label{eq:xnaary3r} \\ s \| ≡ ha − g\floorratio{ha}{g} = ha \bmod g \\ x_\text{upper} \| = \floorratio{gy + dg − ds − 1}{f} \label{eq:ynaarx3r} \\ c_\text{upper} \| = qx_\text{upper} + d\floorratio{hx_\text{upper} + s}{g} \\ z_\text{upper} \| = y − c_\text{upper} \\ ζ \| = \floorratio{z_\text{upper}}{q + d} \\ x \| = x_\text{upper} + ζ \\ z \| = y − c = y − qx − d\floorratio{hx + s}{g} \end{align}

### 12.6. Many Kinds of Unequal Months

We now allow there to be more than two kinds of months with different month lengths. In equation \eqref{eq:xnaary3}, $$⌊ψ(x + a)⌋$$ gave the pattern of long months (with a shift as desired). We now allow an arbitrary number of such pattern, each with its own length difference $$d_i$$ (which may now also be negative), relative frequency $$0 \lt ψ_i \lt 1$$, and pattern shift $$a_i$$. Then we find

\begin{equation} \begin{split} y \| = c + z = qx + ∑_{i}d_i ⌊ψ_i(x + a_i)⌋ − ∑_{i}d_i ⌊ψ_{i}a_i⌋ + z \\ \| = qx + ∑_{i}d_i ⌊ψ_{i}x + ψ_{i}a_i − ⌊ψ_{i}a_i⌋⌋ + z \\ \| = qx + ∑_{i}d_i ⌊ψ_{i}x + σ_i⌋ + z \label{eq:xnaary4} \end{split} \end{equation}

\begin{equation} c = qx + ∑_{i}d_i ⌊ψ_{i}x + σ_i⌋ \end{equation}

\begin{equation} σ_i ≡ ψ_{i}a_i − ⌊ψ_{i}a_i⌋ = ψ_{i}a_i \bmod 1 \end{equation}

The derivation of the formulas for the opposite direction goes analogous to that of equation \eqref{eq:ynaarx3}, but now we have to take into account possibly negative $$d_i$$. We find

\begin{align} p \| = q + ∑_{i}d_{i}ψ_{i} \\ c_s \| = ⌊px⌋ = qx + ⌊∑_{i}d_{i}ψ_{i}⌋ \end{align}

\begin{equation} \begin{split} c − c_s \| = ∑_{i}d_i⌊ψ_{i}x + σ_i⌋ − ⌊∑_{i}d_{i}ψ_{i}x⌋ \\ \| = ∑_{i}d_i (ψ_{i}x + σ_i − δ_i) − \left( ∑_{i}d_{i}ψ_{i}x − ε \right) \\ \| = ∑_{i}d_{i}σ_i − ∑_{i}d_{i}δ_i + ε \end{split} \end{equation}

for

\begin{equation} 0 ≤ δ_i, ε \lt 1 \end{equation}

If there is at least one $$d_i \gt 0$$, then

\begin{equation} c − c_s \gt ∑_{i}d_{i}σ_i − ∑_{\gt0}d_i \end{equation}

(where $$∑_{\gt0}d_i$$ is the sum of all $$d_i$$ that are greater than 0) and otherwise

\begin{equation} c − c_s ≥ ∑_{i}d_{i}σ_i \end{equation}

Also

\begin{equation} c − c_s \lt ∑_{i}d_{i}σ_i − ∑_{\lt0}d_i + 1 \end{equation}

Together this yields

\begin{equation} ∑_{i}d_{i}σ_i − ∑_{\gt0}d_i + 1 ≤ y − y_p ≤ ∑_{i}d_{i}σ_i − ∑_{\lt0}d_i \end{equation}

if there are positive $$d_i$$, and otherwise

\begin{equation} ∑_{i}d_{i}σ_i ≤ c − c_s ≤ ∑_{i}d_{i}σ_i − ∑_{i}d_i \end{equation}

We can combine this to

\begin{equation} ∑_{i}d_{i}σ_i − ∑_{\gt0}d_i ≤ c − c_s ≤ ∑_{i}d_{i}σ_i − ∑_{\lt0}d_i \end{equation}

regardless of whether there are positive $$d_i$$. With that,

\begin{equation} c − ∑_{i}d_{i}σ_i + ∑_{\lt0}d_i ≤ c_s ≤ c − ∑_{i}d_{i}σ_i + ∑_{\gt0}d_i \end{equation}

and so

\begin{align} x_\text{lower} \| = \ceilratio{y − ∑_{i}d_{i}σ_i + ∑_{\lt0}d_i}{p} − 1 \\ x_\text{upper} \| = \ceilratio{y − ∑_{i}d_{i}σ_i + ∑_{\gt0}d_i}{p} − 1 \label{eq:ynaarx4} \end{align}

If $$∑_{\gt0}d_i − ∑_{\lt0}d_i \lt p$$ then $$x_\text{lower}$$ and $$x_\text{upper}$$ differ by at most 1. Then

\begin{align} c_\text{upper} \| = qx_\text{upper} + ∑_{i}d_i⌊ψ_{i}x_\text{upper} + σ_i⌋ \\ z_\text{upper} \| = y − c_\text{upper} \\ ζ \| = \floorratio{z_\text{upper}}{q + ∑_{\gt0}d_i} \\ x \| = x_\text{upper} + ζ \label{eq:xζ4} \\ z \| = y − c − y − qx − ∑_{i}d_i⌊ψ_{i}x + σ_i⌋ \end{align}

If all $$ψ_i = h_i/g$$ are ratios of whole numbers with a common denominator $$g$$, then we find

\begin{align} f \| ≡ qg + ∑_{i}d_{i}h_{i} \\ p \| = \frac{f}{g} \\ y \| = c + z = qx + ∑_{i}d_i\floorratio{h_{i}x + s_i}{g} + z \label{eq:xnaary4r} \\ s_i \| ≡ h_{i}a_i − g\floorratio{h_{i}a_i}{g} = h_{i}a_i \bmod g \\ x_\text{upper} \| = \floorratio{gy + g∑_{\gt0}d_i − ∑_{i}d_{i}s_i − 1}{f} \label{eq:ynaarx4r} \\ c_\text{upper} \| = qx_\text{upper} + ∑_{i}d_i\floorratio{h_{i}x_\text{upper} + s_i}{g} \\ z_\text{upper} \| = y − c_\text{upper} \\ ζ \| = \floorratio{z_\text{upper}}{q + ∑_{\gt0}d_i} \\ x \| = x_\text{upper} + ζ \\ z \| = y − c = y − qx − ∑_{i}d_i\floorratio{h_{i}x + s_i}{g} \end{align}

$$g$$ is the least common multiple of the denominators of all $$ψ_i$$, or is a multiple of that. You can take the product of the denominators of all $$ψ_i$$ for $$g$$, but sometimes a smaller value can be used.

Suppose we want a calendar in which each month has 17 days, with each 3rd month getting 2 days extra, and each 5th month getting 3 days extra. Then the first couple of months contain the following number of days: 17, 17, 19, 17, 20, 19, 17, 17, 19, 20, 17, 19, 17, 17, 22.

Then we have

 $${i}$$ 1 2 $${d_i}$$ 2 3 $${ψ_i}$$ 1/3 = 5/15 1/5 = 3/15 $${a_i}$$ 0 0 $${h_i}$$ 5 3 $${s_i}$$ 0 0

for $$g = 15$$ (the product of denominators 3 and 5). Then $$p = (17×15 + 2×5 + 3×3)/15 = 268/15 = 17 + 13/15$$, so $$f = 268$$ and $$y = 17x + 2⌊x/3⌋ + 3⌊x/5⌋$$ and $$x_\text{upper} = ⌊(15y + 15×5 − 0 − 1)/268⌋ = ⌊(15y + 74)/268⌋$$.

The following table shows the results that you get when you calculate for certain calendar dates $$x$$, $$z$$ what the corresponding running day number $$y$$ is, and then calculate from that $$y$$ what the month number $$x = x_\text{upper} + ζ$$ is.

 $${x}$$ 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 $${z}$$ 0 16 0 16 0 18 0 16 0 19 0 18 0 16 0 16 0 18 0 19 0 16 0 18 0 16 0 16 0 21 0 16 $${y}$$ 0 16 17 33 34 52 53 69 70 89 90 108 109 125 126 142 143 161 162 181 182 198 199 217 218 234 235 251 252 273 274 290 $${x_\text{upper}}$$ 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 $${y_\text{upper}}$$ 0 17 17 34 34 53 53 70 70 90 90 109 109 126 126 143 143 162 162 182 182 199 199 218 218 235 235 252 252 274 274 291 $${ζ}$$ 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 0 −1 $${x_\text{upper} + ζ}$$ 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15

### 12.7. Very Unequal Month Lengths

The preceding methods for calculating the calendar date from the running day number for calendars with very unequal month lengths assumed that the variation in month lengths is less than the average month length: $$d ≤ p$$ when there are two different month lengths, or $$∑_{\gt0}d_i − ∑_{\lt0}d_i \lt p$$ if there are more month lengths. What to do if that condition is not met?

In that case you'll have to find the correct month by searching for it. First try month number $$x = x_\text{upper}$$ and calculate the corresponding $$z = y − c$$ and $$ζ$$. Is that $$ζ \lt 0$$? Then add $$ζ$$ to $$x$$ and try again, until $$ζ = 0$$: then you have found the correct $$x$$.

Let $$q = 3$$, $$d = 7$$, $$ψ = 1/3$$, then $$h = 1$$, $$g = 3$$, $$f = 16$$, $$p = q + dψ = 3 + 5/3 = 5 \frac{2}{3}$$ so the condition $$d ≤ p$$ is not satisfied. In this calendar the first three months have length $$3, 3, 10$$ days, and that pattern repeats every three months. For this calendar, we have

\begin{align*} x_\text{upper} \| = \floorratio{3y + 20}{16} \\ x_\text{lower} \| = \floorratio{3y + 2}{16} \\ c \| = 3x + 7\floorratio{x}{3} \end{align*}

We find

 $${y}$$ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $${x_\text{upper}}$$ 1 1 1 1 2 2 2 2 2 2 3 3 3 3 4 4 4 $${c_\text{upper}}$$ 3 3 3 3 6 6 6 6 6 6 16 16 16 16 19 19 19 $${z_0}$$ −3 −2 −1 0 −2 −1 0 1 2 3 −6 −5 −4 −3 −5 −4 −3 $${ζ_0}$$ −1 −1 −1 0 −1 −1 0 0 0 0 0 0 0 0 −1 −1 0 $${c_1}$$ 0 0 0 3 3 6 6 6 6 16 16 16 $${z_1}$$ 0 1 2 1 2 4 5 6 7 −2 −1 0 $${ζ_1}$$ 0 0 0 0 0 0 0 0 0 −1 −1 0 $${c_2}$$ 6 6 $${z_2}$$ 8 9 $${ζ_2}$$ 0 0 $${x}$$ 0 0 0 1 1 1 2 2 2 2 2 2 2 2 2 2 3 $${z}$$ 0 1 2 0 1 2 0 1 2 3 4 5 6 7 8 9 10

For example: for $$y = 11$$ we find

\begin{align*} x_\text{upper} \| = \floorratio{3×11 + 20}{16} = 3 \\ c_\text{upper} \| = 3×3 + 7\floorratio{3}{3} = 16 \\ z_0 \| = y − c_\text{upper} = 11 − 16 = −5 \end{align*}

That $$z_0$$ is negative, so we decrement $$x$$ by one ($$x = 3 − 1 = 2$$) and try again. With the new $$x$$ we find

\begin{align*} c_1 \| = 3×2 + 7\floorratio{2}{3} = 6 \\ z_1 \| = y − c_1 = 11 − 6 = 5 \end{align*}

which is no longer negative, so we're done. For $$y = 11$$ we find $$x = 2$$ and $$z = 5$$.

If you want to, then you can use this procedure also if $$d ≤ p$$.

The greatest number of months that you may have to try before you find the right one is equal to the greatest value that $$x_\text{upper} − x_\text{lower}$$ can attain, plus 1. For calendars with two different month lengths we have

\begin{equation} x_\text{upper} − x_\text{lower} = \ceilratio{y − dσ + d}{p} − \ceilratio{y − dσ + 1}{p} \end{equation}

The greatest value that this can attain is $$⌈(d − 1)/p⌉$$, so the number $$N$$ of month that you have to try at most is equal to

\begin{equation} N = \ceilratio{d − 1}{p} + 1 \end{equation}

For calendars with more than two different month lengths we have

\begin{equation} x_\text{upper} − x_\text{lower} = \ceilratio{y − ∑_{i}d_{i}σ_i + ∑_{\gt0}d_i}{p} − \ceilratio{y − ∑_{i}d_{i}σ_i + ∑_{\lt0}d_i}{p} \end{equation}

which has for its greatest possible value $$⌈(∑_{\gt0}d_i − ∑_{\lt0}d_i)/p⌉$$ so for such a calendar

\begin{equation} N = \ceilratio{∑_{\gt0}d_i − ∑_{\lt0}d_i}{p} + 1 \end{equation}

### 12.8. Month Lengths Without Internal Patterns

Previously we looked at calendars of which the month lengths can be expressed as the sum of various patterns, for example with an extra day every second month and two additional days every third month, so that each sixth month gets three extra days in total. But what if the months repeat after some time but show no other discernable pattern?

As an example we use a calendar with the following month lengths, that keep repeating themselves indefinitely: 7, 13, 5, 11, 4 days. The "year" is 7 + 13 + 5 + 11 + 4 = 40 days long. The relationship between running day number $$y$$, running month number $$x$$, and day number $$z$$ in the current month (all starting at 0) is then

 $${y}$$ 0 1 …  6 7 …  19 20 …  24 25 …  35 36 …  39 $${x}$$ 0 0 …  0 1 …  1 2 …  2 3 …  3 4 …  4 $${z}$$ 0 1 …  6 0 …  12 0 …  4 0 …  10 0 …  3

The calendar has $$f$$ days in $$g$$ months, for an average month length of $$p = f/g$$ days. The relationship between the running day number $$y$$ and the running month number $$x$$ is a step function: after a (varying) number of days, the month number increases by one. Such a step of 1 that occurs just before day $$a$$ and repeats itself every $$f$$ days can be obtained through the formula

$\floorratio{y − a}{f}$

For the example calendar we have $$f = 40$$ and $$g = 5$$ and there is (amongst others) a step just before day $$a = 7$$. We get that with

$x = \floorratio{y − 7}{40}$

 $${y}$$ 0 1 …  6 7 8 …  46 47 48 $${y−7}$$ −7 −6 −1 0 1 39 40 41 $${x}$$ −1 −1 −1 0 0 0 1 1

When there are $$g$$ months, each with its own $$a_i$$ (for $$i$$ from 1 through $$g$$), then their combined effect is

\begin{equation} x = g + ∑_i\floorratio{y − a_i}{f} \end{equation}

If the successive month lengths are $$m_i$$, then the first step is at $$a_1 = 0$$, the second one is at $$a_2 = a_1 + m_1 = m_1$$, the third one is at $$a_3 = a_2 + m_2 = m_1 + m_2$$, and in general

\begin{equation} a_i = ∑_{j\lt i}m_j \end{equation}

Then

\begin{equation} x = g + ∑_i\floorratio{y − ∑_{j\lt i}m_j}{f} \label{eq:willekeurigynaarx} \end{equation}

For the example calendar we have $$f = 40$$, $$g = 5$$, and

 $${i}$$ 1 2 3 4 5 $${m_i}$$ 7 13 5 11 4 $${a_i}$$ 0 7 20 25 36

and so

\begin{align*} x \| = \floorratio{y + 40 − 0}{40} + \floorratio{y + 40 − 7}{40} + \floorratio{y + 40 − 20}{40} + \floorratio{y + 40 − 25}{40} + \floorratio{y + 40 − 36}{40} \\ \| = \floorratio{y + 40}{40} + \floorratio{y + 33}{40} + \floorratio{y + 20}{40} + \floorratio{y + 15}{40} + \floorratio{y + 4}{40} \end{align*}

For example, if $$y = 21$$ then

$x = \floorratio{61}{40} + \floorratio{54}{40} + \floorratio{41}{40} + \floorratio{36}{40} + \floorratio{25}{40} = 1 + 1 + 1 + 0 + 0 = 3$

If we go in the other direction then we also have a staircase, but now days and months exchange roles as length and height of the steps. With that, the formula for the running day number $$c$$ of the first day of month $$x$$ is:

\begin{equation} c = ∑_{i}m_i\floorratio{x + g − i}{g} \end{equation}

With again the same calendar, we find

$c = 7 \floorratio{x + 4}{5} + 13 \floorratio{x + 3}{5} + 5 \floorratio{x + 2}{5} + 11 \floorratio{x + 1}{5} + 4 \floorratio{x}{5}$

$${x}$$ 0 1 2 3 4 5
$${7⌊(x+4)/5⌋}$$ 0 7 7 7 7 7
$${{13⌊(x+3)/5⌋}}$$ 0 0 13 13 13 13
$${5⌊(x+2)/5⌋}$$ 0 0 0 5 5 5
$${11⌊(x+1)/5⌋}$$ 0 0 0 0 11 11
$${4⌊x/5⌋}$$ 0 0 0 0 0 4
$${c}$$ 0 7 20 25 36 40

The formula to go from running month number $$x$$ and day number $$z$$ in the current month to running day number $$y$$ (all beginning at 0) is then (with $$i$$ running from 1 through $$g$$)

\begin{equation} y = c + z = ∑_{i}m_i\floorratio{x + g − i}{g} + z \label{willekeurigxnaary} \end{equation}

These formulas are, if you write out the summation, a lot longer than the formulas that we found earlier for calendars with internal patterns, so it is convenient if you recognize such patterns for a calendar, but not every calendar has such patterns.

The simple calendar from section 12.3 has $$y = ⌊px + σ⌋ + z$$. To have that calendar begin month $$x$$ at day $$y$$ we need $$y = ⌊px + σ⌋$$, from which follows $$y ≤ px + σ \lt y + 1$$, hence (for $$x \gt 0$$) $$(y − σ)/x ≤ p \lt (y + 1 − σ)/x$$. We know that $$0 ≤ σ \lt 1$$, so

$\frac{y − 1}{x} \lt \frac{y − σ}{x} ≤ p \lt \frac{y + 1 − σ}{x} \lt \frac{y + 1}{x}$

so

\begin{equation} \frac{y − 1}{x} \lt p \lt \frac{y + 1}{x} \label{eq:beperkp} \end{equation}

Not every $$p$$ that meets these restrictions yields a simple calendar, but if a $$p$$ does not meet these restrictions for at least one of its months, then there is certainly no simple calendar for these month lengths.

Our example calendar does not meet the restrictions that are necessary for the simple formulas from section 12.3 to apply, because there are more than two different month lengths, even if we leave the last month out of consideration. We can also see this using equation \eqref{eq:beperkp}. The first day of month $$x = 1$$ has $$y = 7$$, so we must have $$6 \lt p \lt 8$$. The first day of month $$x = 2$$ has $$y = 20$$ so we need $$9\frac{1}{2} = 19/2 \lt p \lt 21/2 = 10\frac{1}{2}$$, and already we're in trouble, because to have the beginning of month $$x = 1$$ in the right place $$p$$ must be less than 8, but to have the beginning of month $$x = 2$$ in the right place $$p$$ must be greater than 9½, and those restrictions cannot be met at the same time.

### 12.9. Combinations of straight lines

It is rare for a calendar to be fully defined by just one period, so usually you have to combine several periods. Let's assume that we have two straight lines for two periods:

\begin{align} y_1 \| = c_1(x_1) + z_1 \\ y_2 \| = c_2(x_2) + z_2 \end{align}

and in the other direction

\begin{align} x_1 \| = c_1'(y_1) \\ z_1 \| = y_1 − c_1(x_1) \\ x_2 \| = c_2'(y_2) \\ z_2 \| = y_2 − c_2(x_2) \end{align}

where $$c'(y)$$ is the inverse of $$c(x)$$: $$c'(c(x)) = x$$ and $$c(c'(y)) = y$$.

In simple cases we have

\begin{eqnarray} \{ x_1, r_1 \} \| = \Div(g_1y_1 + g_1 − 1 − s_1, f_1) \\ z_1 \| = \floorratio{r_1}{g_1} \\ \{ x_2, r_2 \} \| = \Div(g_2y_2 + g_2 − 1 − s_2, f_2) \\ z_2 \| = \floorratio{r_2}{g_2} \end{eqnarray}

We assume that the first line is for the smaller period and the second line is for the larger period. There are two ways to combine these: We can equate $$y_1$$ to $$z_2$$ or to $$x_2$$. In the first case, the two periods have the same leap unit, for example days. In the second case, the two periods have different leap units (for example, sometimes an extra day for the first period, and sometimes an extra month for the second period). If the larger period needs no leap rules at all, then you can choose which combination method to use.

Let's call the first case the "flat" combination, because the leap units remain the same. Let's call the second case the "stepped" combination, because the leap unit goes a step higher.

The combination of months and years in most (perhaps all) solar calendars (such as the Gregorian calendar, the Julian calendar, and the Egyptian calendar) is flat, i.e. of the first kind. A month can be a day shorter or longer than another month (and exactly one month can be a lot shorter), and a year can be a day shorter or longer than another year. The number of days in a year does not depend on the months, because the rules to calculate the length of the year depend on the year number but not on the month number.

The combination of months and years in a lunisolar calendar (such as the Hebrew and Babylonian calendars) can be flat with very unequal year lengths ($$d \gt 1$$) or stepped (then usually $$d = 1$$). A month can be a day longer or shorter than another month, and a year can be a month shorter or longer than another month. (For the flat combination, one month of the year can be much shorter.) In this way you can follow two separate (astronomical) cycles: the motion of the Sun (with the year) and the motion of the Moon (with the month). If the combination is stepped, then the length of the year depends on the length of the months, because the year is then defined in terms of a fixed number of months, not a fixed number of days.

Lunar calendars that are not lunisolar (such as the administrative Islamic calendar) usually do not have any leap rules, so then both methods can be used.

If a calendar has more than two important large periods with leap rules (for example, not just for the month and the year, but also for the century), then it is possible that some combinations are flat and others are stepped.

If we have to deal with more than one period, then it is important for translating a running day number into a calendar date to have the running numbers begin at 0 for each of those periods.

As an example we'll take for the first calendar a simple one with $$p_1 = 7/3 = 2 \frac{2}{3}$$ and for the second calendar a simple one with $$p_2 = 37/5 = 7 \frac{2}{5}$$. Then we have

\begin{align*} \\ y_1 \| = \floorratio{7x_1}{3} + z_1 = c_1 + z_1 \\ y_2 \| = \floorratio{37x_2}{5} + z_2 = c_2 + z_2 \\ x_1 \| = \floorratio{3y_1 + 2}{7} \\ z_1 \| = \floorratio{(3y_1 + 2) \bmod 7}{3} \\ x_2 \| = \floorratio{5y_2 + 4}{37} \\ z_2 \| = \floorratio{(5y_2 + 4) \bmod 37}{5} \end{align*}

or

\begin{align*} \{x_1, r_1\} \| = \Div(3y_1 + 2, 7) \\ z_1 \| = \floorratio{r_1}{3} \\ \{x_2, r_2\} \| = \Div(5y_2 + 4, 37) \\ z_2 \| = \floorratio{r_2}{5} \end{align*}

#### 12.9.1. Flat combination

In this case $$z_2 = y_1$$, so

\begin{equation} y_2 = c_2(x_2) + c_1(x_1) + z_1 \label{eq:vlak} \end{equation}

and in the other direction

\begin{align} x_2 \| = c_2'(y_2) \label{eq:vlakr} \\ y_1 \| = z_2 = y_2 − c_2(x_2) \\ x_1 \| = c_1'(y_1) \\ z_1 \| = y_1 − c_1(x_1) \end{align}

For simple cases

\begin{align} \{x_2, r_2\} \| = \Div(g_2y_2 + g_2 − 1 − s_2, f_2) \\ \{x_1, r_1\} \| = \Div\left( g_1\floorratio{r_2}{g_2} + g_1 − 1 − s_1, f_1 \right) \\ z_1 \| = \floorratio{r_1}{g_1} \end{align}

Here (for example) $$y_2$$ is the running day number, $$x_2$$ is the year number, $$y_1 = z_2$$ is the day number in the current year, $$x_1$$ is the month number in the current year (with 0 for the first month), and $$z_1$$ is the day number in the current month (with 0 for the first day).

To calculate the calendar date from the running day number, we must first calculate the larger period ($$x_2$$, $$z_2$$), and then the smaller period ($$x_1$$, $$z_1$$).

The two example calendars then yield, for the first 20 days,

$${y_2}$$ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$${x_2}$$ 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2
$${r_2}$$ 4 9 14 19 24 29 34 2 7 12 17 22 27 32 0 5 10 15 20 25
$${z_2}$$ 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5
$${y_1}$$ 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5
$${x_1}$$ 0 0 1 1 2 2 2 0 0 1 1 2 2 2 0 0 1 1 2 2
$${r_1}$$ 2 5 1 4 0 3 6 2 5 1 4 0 3 6 2 5 1 4 0 3
$${z_1}$$ 0 1 0 1 0 1 2 0 1 0 1 0 1 2 0 1 0 1 0 1

Running day number $$y_2 = 16$$ corresponds to day $$z_1 = 0$$ of month $$x_1 = 1$$ of year $$x_2 = 2$$.

#### 12.9.2. Stepped combination

In this case $$x_2 = y_1$$, so

\begin{equation} y_2 = c_2(c_1(x_1) + z_1) + z_2 \end{equation}

and in the other direction

\begin{align} y_1 \| = c_2'(y_2) \\ z_2 \| = y_2 − c_2(y_1) \\ x_1 \| = c_1'(y_1) \\ z_1 \| = y_1 − c_1(x_1) \end{align}

For simple cases

\begin{align} \{y_1, r_2\} \| = \Div(g_2y_2 + g_2 − 1 − s_2, f_2) \\ z_2 \| = \floorratio{r_2}{g_2} \\ \{x_1, r_1\} \| = \Div(g_1y_1 + g_1 − 1 − s_1, f_1) \\ z_1 \| = \floorratio{r_1}{g_1} \end{align}

Here (for example) $$y_2$$ is the running day number, $$x_1$$ is the year number, $$x_2 = y_1$$ is the running month number, $$z_1$$ is the month number in the current year (with 0 for the first month), and $$z_2$$ is the day number in the current month (with 0 for the first day).

With our two example calendars we find

$${y_2}$$ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$${x_2}$$ 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2
$${r_2}$$ 4 9 14 19 24 29 34 2 7 12 17 22 27 32 0 5 10 15 20 25
$${z_2}$$ 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5
$${y_1}$$ 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2
$${x_1}$$ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
$${r_1}$$ 2 2 2 2 2 2 2 5 5 5 5 5 5 5 1 1 1 1 1 1
$${z_1}$$ 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0

Running day number $$y_2 = 16$$ corresponds to day $$z_2 = 2$$ of month $$z_1 = 0$$ of year $$x_1 = 1$$.

### 12.10. Simultaneous Cycles

Most calendars have higher and lower periods, and the number of a higher period changes much less often than the number of a lower period. After the 7th day of the 3rd month follows the 8th day of the 3rd month − the month number changes less often than the day number.

Some calendars have different periods that change simultaneously. The most common calendars often have such a case, too, in the form of weekdays and days of the month. When the next day arrives, then the day number of the month changes, but the weekday changes, too. After Monday the 7th we get Tuesday the 8th − the weekday and the day number change equally fast.

We don't need the weekday to be able to point to a unique day (30 August 2011 indicates exactly one day; for that we do not need to know that that day was a Tuesday), so the weekday does not usually play a role in calendar calculations. However, there are calendars (for example the calendars of Central America) which do use simultaneous cycles to point at particular days. We look at this type of calendar below.

Suppose that a calendar uses simultaneous periods $$p_i$$ for $$i$$ from 1 through $$n$$. The day number in each period begins at 0. We write a date in that calendar as $$\{x\} = \{x_1,x_2,…,x_n\}$$, where $$x_i$$ is the day number from period $$i$$. If $$x_i$$ is equal to $$p_i − 1$$, then the next day has $$x_i = 0$$ again.

For example, in a calendar with $$p_1 = 13$$ and $$p_2 = 20$$, we get after day $$\{11,8\}$$ the following days: $$\{12,9\}$$, $$\{0,10\}$$, $$\{1,11\}$$, …, $$\{9,19\}$$, $$\{10,0\}$$, $$\{11,1\}$$.

#### 12.10.1. From Running Day Number to Date

To translate running day number $$J$$ into $$\{x\}$$ we can use the following formula:

\begin{equation} x_i = (J + a_i) \bmod p_i \label{eq:cycli} \end{equation}

Here $$a_i$$ is the value of $$x_i$$ when $$J = 0$$. That value depends on the calendar.

Suppose that in the calendar from the previous example $$J = 0$$ corresponds to $$\{11,8\}$$. Then $$a_1 = 11$$ and $$a_2 = 8$$, and then

\begin{align*} x_1 = (J + 11) \bmod 13 \\ x_2 = (J + 8) \bmod 20 \end{align*}

Day $$J = 11$$ then corresponds to $$x_1 = (11 + 11) \bmod 13 = 22 \bmod 13 = 9$$ and $$x_2 = (11 + 8) \bmod 20 = 19 \bmod 20 = 19$$, so $$\{9,19\}$$.

#### 12.10.2. From Date to Running Day Number

It is a lot more difficult to go in the other direction. Then we have to find a $$J$$ for which equation \eqref{eq:cycli} is satisfied for all $$i$$, i.e.,

\begin{equation} J ≡ x_i − a_i \pmod{p_i} \end{equation}

We define

\begin{equation} c_i = x_i − a_i \end{equation}

We first look at the case $$n = 2$$. Then we need to solve $$J$$ from

\begin{align} J \| ≡ c_1 \pmod{p_1} \label{eq:c1} \\ J \| ≡ c_2 \pmod{p_2} \label{eq:c2} \end{align}

All solutions to equation \eqref{eq:c1} are of the kind

\begin{equation} J = c_1 + kp_1 \end{equation}

when $$k$$ runs through all whole numbers. Substituting into formula \eqref{eq:c2} yields

\begin{equation} kp_1 ≡ c_2 − c_1 \pmod{p_2} \label{eq:kp_1} \end{equation}

If we could find a number $$r_2$$ such that $$r_2p_1 ≡ 1 \pmod{p_2}$$, then we could solve the preceding formula for $$k$$. Such a number $$r_2$$ only exists if $$p_1$$ and $$p_2$$ are relative prime, which means that they have no common divisors (greater than 1).

If the greatest common divisor of $$p_1$$ and $$p_2$$ is equal to $$g$$ (greater or equal to 1), and

\begin{equation} q_i = \frac{p_i}{g} \end{equation}

then we can rewrite formula \eqref{eq:kp_1} to

\begin{equation} kgq_1 ≡ c_2 − c_1 \pmod{gq_2} \end{equation}

If $$c_2 − c_1$$ is evenly divisible by $$g$$, then we can divide by $$g$$ throughout and then we find

\begin{equation} kq_1 = \frac{c_2 − c_1}{g} \pmod{q_2} \label{eq:red} \end{equation}

For dates from calendars based on formula \eqref{eq:cycli} $$c_2 − c_1$$ is evenly divisible by $$g$$, so we make that assumption.

Because $$q_1$$ and $$q_2$$ are relative prime, there is a number $$r_2$$ such that $$r_2q_1 ≡ 1 \pmod{q_2}$$. With that we find from equation \eqref{eq:red}

\begin{equation} k ≡ kq_1r_2 ≡ \frac{c_2 − c_1}{g} r_2 \pmod{q_2} \end{equation}

and then

\begin{align} kp_1 \| ≡ q_1r_2\frac{c_2 − c_1}{g} \pmod{q_2p_1} \\ J \| = c_1 + kp_1 ≡ c_1 + q_1r_2(c_2 − c_1) ≡ c_1 (1 − r_2q_1) + c_2r_2q_1 \pmod{q_2p_1} \end{align}

thus

\begin{equation} J ≡ (x_1 − a_1) (1 − r_2q_1) + (x_2 − a_2)r_2q_1 \pmod{p_2q_1} \label{eq:cyclitoj} \end{equation}

Summarizing:

1. Calculate the greatest common divisor $$g$$ of $$p_1$$ and $$p_2$$:

\begin{equation} g = \gcd(p_1, p_2) \end{equation}

2. Find the smallest multiple $$s$$ of $$p_1/g$$ for which

$s ≡ 1 \pmod{p_2/g}$

3. Then

\begin{equation} J ≡ sx_2 + (1 − s)x_1 − (sa_2 + (1 − s)a_1) \pmod{p_1p_2/g} \end{equation}

How can you find a $$r_2$$ such that $$s = r_2q_1 ≡ 1 \pmod{q_2}$$? Because you only need to find that $$r_2$$ or $$s$$ once for a given calendar, it is usually the least amount of work to just try successive values until you find the correct one. You need to check at most $$q_2$$ successive values. If you need to calculate such $$r_2$$ often, then you can use the Extended Euclidean Algorithm.

You can find the greatest common divisor of $$p_1$$ and $$p_2$$ by using the regular Euclidean Algorithm.

We again use the calendar from the previous example, with $$p_1 = 13$$, $$p_2 = 20$$, $$a_1 = 11$$ and $$a_2 = 8$$. The greatest common divisor of 13 and 20 is 1, so $$g = 1$$. Now we seek $$s$$. That is the smallest multiple of $$p_1/g = 13/1 = 13$$ for which $$s ≡ 1 \pmod{20}$$, which is equivalent to $$s \bmod 20 = 1$$.

 $${k}$$ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $${s = 13k}$$ 0 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 208 221 234 247 260 $${s \bmod 20}$$ 0 13 6 19 12 5 18 11 4 17 10 3 16 9 2 15 8 1 14 7 0

We find $$s = 17×13 = 221$$, because $$221 ≡ 1 \pmod{20}$$.

Then

\begin{align*} J \| ≡ 221x_2 − 220x_1 − (221×8 − 220×11) \\ \| ≡ 221x_2 − 220x_1 + 652 \\ \| ≡ 221x_2 + 40x_1 + 132 \\ \| ≡ 40x_1 + 221x_2 + 132 \pmod{13×20 = 260} \end{align*}

For date $$\{9,19\}$$ we then find

\begin{align*} J \| ≡ 40×9 + 221×19 + 132 \\ \| ≡ 360 + 4199 + 132 \\ \| ≡ 100 + 39 + 132 ≡ 271 ≡ 11 \pmod{260} \end{align*}

so day $$J = 11$$ and every 260 days earlier or later correspond to date $$\{9,19\}$$.

The order of the two congruence relations that we began with is not important, so we can switch the roles of $$p_1$$ and $$p_2$$ (and of $$a_1$$ and $$a_2$$). We seek the smallest multiple $$s$$ of $$p_2/g = 20/1 = 20$$ for which $$s ≡ 1 \pmod{13}$$, which means $$s \bmod 13 = 1$$. We find $$s = 40$$ because $$40 = 3×13 + 1 ≡ 1 \pmod{13}$$. Then

\begin{align*} J \| ≡ 40x_1 ― 39x_2 − (40×11 − 39×8) \\ \| ≡ 40x_1 ― 39x_2 − 128 \\ \| = 40x_1 + 221x_2 + 132 \pmod{260} \end{align*}

which is the same as before.

Warning! Formula \eqref{eq:cyclitoj} only gives useful results for $$\{x\}$$ that really occur in the calendar. If $$g$$ is not equal to 1, then not all possible $$\{x\}$$ can occur. If you enter an $$\{x\}$$ that does not occur in the calendar, then you get a reasonable-looking result from the formula, but that result then still won't be valid.

Now we look at a calendar for which the periods are not relatively prime, with $$p_1 = 10$$, $$p_2 = 15$$, $$a_1 = a_2 = 0$$. Then

\begin{align*} x_1 = J \bmod 10 \\ x_2 = J \bmod 15 \end{align*}

The largest common divisor of $$p_1$$ and $$p_2$$ is $$g = 5$$. We seek the smallest multiple $$s$$ of $$p_1/g = 10/5 = 2$$ for which $$s ≡ 1 \pmod{15/5 = 3}$$. That is $$s = 4$$, because $$4 = 2×2 = 3 + 1 ≡ 1 \pmod{3}$$. With that,

\begin{align*} J \| ≡ 4x_2 − 3x_1 − 0 ≡ 27x₁ + 4x₂ \pmod{10×15/5 = 30} \end{align*}

For $$J$$ from 8 to 17 we then find for $$\{x\}$$: $$\{8,8\}$$, $$\{9,9\}$$, $$\{0,10\}$$, $$\{1,11\}$$, $$\{2,12\}$$, $$\{3,13\}$$, $$\{4,14\}$$, $$\{5,0\}$$, $$\{6,1\}$$, $$\{7,2\}$$.

For that $$\{x\}$$ we find:

 $${x_1}$$ 8 9 0 1 2 3 4 5 6 7 $${x_2}$$ 8 9 10 11 12 13 14 0 1 2 $${27x_1 + 4x_2}$$ 248 279 40 71 102 133 164 135 166 197 $${J \bmod 30}$$ 8 9 10 11 12 13 14 15 16 17

Because $$g$$ is not equal to 1, many combinations of $$x_1$$ and $$x_2$$ do not occur in this calendar. Only combinations for which $$x_1 − x_2$$ is evenly divisible by 5 occur in this calendar. For example, $$\{3,7\}$$ does not occur. If we apply the equation for $$J$$ to that date, then we find $$J ≡ 27×3 + 4×7 ≡ 19 \pmod{30}$$, but for $$J = 19$$ we find $$x_1 = 9$$ and $$x_2 = 4$$, so $$\{9,4\}$$ and not $$\{3,7\}$$. For impossible $$\{x\}$$ we still get nice-looking $$J$$ out of the formula.

#### 12.10.3. More than Two Periods

Using equation \eqref{eq:cyclitoj}, $$J ≡ x_1 − a_1 \pmod{p_1}$$ and $$J ≡ x_2 − a_2 \pmod{p_2}$$ lead to another equation $$J ≡ C_2 \pmod{P_2}$$ with

\begin{align} C_2 \| ≡ (x_1 − a_1) (1 − s_2) + (x_2 − a_2) s_2 \\ P_2 \| ≡ p_1p_2/g_1 \end{align}

That formula has the same form as the two formulas that we started with, so we can combine that formula in the same manner with $$J ≡ x_3 − a_3 \pmod{p_3}$$, and so on until we've handled all $$p_i$$. We end up with a formula that again has the same form $$J ≡ C_n \pmod{P_n}$$, and that is the solution of the $$n$$ equations.

The procedure is then as follows. Given $$x_i$$, $$p_i$$, $$a_i$$ for $$i$$ from 1 through $$n$$,

1. Assign $$C_1 = x_1 − a_1$$ and $$P_1 = p_1$$.
2. For $$i$$ from 2 through $$n$$:
3. Determine $$g_i = \gcd(P_{i−1}, p_i)$$, the greatest common divisor of $$P_{i−1}$$ and $$p_i$$.
4. Find the smallest $$s_i$$ that is a multiple of $$P_{i−1}/g_i$$ and that satisfies $$s_i ≡ 1 \pmod{p_i/g_i}$$.
5. Assign $$C_i = C_{i−1} (1 − s_i) + (x_i − a_i) s_i$$ and $$P_i = P_{i−1}p_i/g_i$$.
6. The solution is \begin{equation} J ≡ C_n \pmod{P_n} \label{eq:cycle} \end{equation}

Let's look at a calendar with the following characteristics:

 $${i}$$ 1 2 3 $${p_i}$$ 4 5 6 $${a_i}$$ 3 1 2

You translate running day number $$J$$ to calendar date $$\{x\}$$ as follows:

\begin{align*} x_1 \| = (J + 3) \bmod 4 \\ x_2 \| = (J + 1) \bmod 5 \\ x_3 \| = (J + 2) \bmod 6 \end{align*}

In the opposite direction we find:

\begin{align*} C_1 \| = x_1 − 3 \\ P_1 \| = 4 \\ g_2 \| = \gcd(4, 5) = 1 \\ s_2 \| = 16 = 4×4 = 3×5 + 1 ≡ 1 \pmod{5} \\ C_2 \| = C_1×(1 − 16) + (x_2 − 1)×16 \\ \| = −15×(x_1 − 3) + 16×(x_2 − 1) \\ \| = −15x_1 + 16x_2 + 29 \\ P_2 \| = 4×5/1 = 20 \\ g_3 \| = \gcd(20, 6) = 2 \\ s_3 \| = 10 = 5×2 = 3×3 + 1 ≡ 1 \pmod{3} \\ C_3 \| = C_2×(1 − 10) + (x_3 − 2)×10 \\ \| = −9×(−15x_1 + 16x_2 + 29) + 10×(x_3 − 2) \\ \| = 135x_1 − 144x_2 + 10x_3 − 281 \\ P_3 \| = 20×6/2 = 60 \end{align*}

The solution is $$J ≡ C_3 ≡ 15x_1 + 36x_2 + 10x_3 + 19 \pmod{60}$$.

Let's check: if $$J = 654$$, then $$x_1 = (654 + 3) \bmod 4 = 1$$, $$x_2 = (654 + 1) \bmod 5 = 0$$, $$x_3 = (654 + 2) \bmod 6 = 2$$. And from $$\{x\}$$ to $$J$$ we find $$J ≡ 15×1 + 36×0 + 10×2 + 19 ≡ 54 \pmod{60}$$, which is correct, because $$54 ≡ 654 \pmod{60}$$.

#### 12.10.4. To One Solution

Because the date $$\{x\}$$ is given as a collection of positions in repeating periods, there is an infinite number of days that fit that date. For the running day number $$J$$ that corresponds to date $$\{x\}$$ we find formula \eqref{eq:cycle}, which gives a solution $$\bmod P_n$$. If you have found a running day number $$J$$ that fits date $$\{x\}$$, then you can add or subtract arbitrary multiples of $$P_n$$ and then you find another $$J$$ that belongs to date $$\{x\}$$. How do we pick the right one?

To pick the right solution out of an infinite collection of solutions, we need to use additional information. One appropriate way is to pick the solution that is closest to a date $$J_0$$ chosen by us. One can interpret "closest to" in at least four different ways:

1. the last one at or before $$J_0$$
2. the first one at or after $$J_0$$
3. the last one before $$J_0$$
4. the first one after $$J_0$$

We investigate these cases one by one, for $$J ≡ C_n \pmod{P_n}$$.

##### 12.10.4.1. The last one at or before

We seek the last $$J$$ at or before $$J_0$$ that satisfies $$J ≡ C_n \pmod{P_n}$$. Then, with $$k$$ a whole number,

\begin{align} J_0 − P_n \| \lt J ≤ J_0 \\ J_0 − P_n \| \lt C_n + kP_n ≤ J_0 \\ \frac{J_0 − C_n}{P_n} − 1 \| \lt k ≤ \frac{J_0 − C_n}{P_n} \end{align}

There is only one whole number $$k$$ that satisfies this inequality, and that is

\begin{equation} k = \floorratio{J_0 − C_n}{P_n} \end{equation}

so

\begin{equation} \begin{split} J \| = C_n + P_n \floorratio{J_0 − C_n}{P_n} \\ \| = C_n + P_n \left( \frac{J_0 − C_n}{P_n} − \mod1ratio{J_0 − C_n}{P_n} \right) \\ \| = C_n + (J_0 − C_n) − P_n \mod1ratio{J_0 − C_n}{P_n} \\ \| = J_0 − ⌊J_0 − C_n⌉_{P_n} \\ \| = J_0 − ((J_0 − C_n) \bmod P_n) \end{split} \label{eq:nearestle} \end{equation}

For example, what is the last day at or before $$J_0 = 700$$ that corresponds to date $$\{1,0,2\}$$ in the calendar from the previous example?

There we found $$C_n = 15x_1 + 36x_2 + 10x_3 + 19$$ and $$P_n = 60$$, so $$C_n = 15×1 + 36×0 + 10×2 + 19 = 54$$. With formula \eqref{eq:nearestle} we then find $$J = 700 − ((700 − 54) \bmod 60) = 700 − (646 \bmod 60) = 700 − 46 = 654$$ which is correct: 654 + 60 = 714 is greater than 700, so 654 is the last $$J$$ that is less than or equal to $$J_0$$ and that fits date $$\{1,0,2\}$$.

We should find $$J = 654$$ for all $$J_0$$ from 654 through 654 + 60 − 1 = 713, and we do:

 $${J_0}$$ $${J_0 − C_n}$$ $${(J_0 − C_n) \bmod P_n}$$ $${J}$$ 653 599 59 594 654 600 0 654 712 658 58 654 713 659 59 654 714 660 0 714

##### 12.10.4.2. The first at or after

We seek the first $$J$$ at or after $$J_0$$ that satisfies $$J ≡ C_n \pmod{P_n}$$. Then, with $$k$$ a whole number,

\begin{align} J_0 \| ≤ J \lt J_0 + P_n \\ J_0 \| ≤ C_n + kP_n \lt J_0 + P_n \\ \frac{J_0 − C_n}{P_n} \| ≤ k \lt \frac{J_0 − C_n}{P_n} + 1 \end{align}

There is only one whole number $$k$$ that satisifies this inequality, and that number is

\begin{equation} k = \ceilratio{J_0 − C_n}{P_n} \end{equation}

so

\begin{equation} \begin{split} J \| = C_n + P_n \ceilratio{J_0 − C_n}{P_n} \\ \| = C_n + P_n \left( \frac{J_0 − C_n}{P_n} + \dom1ratio{J_0 − C_n}{P_n} \right) \\ \| = C_n + (J_0 − C_n) + P_n \dom1ratio{J_0 − C_n}{P_n} \\ \| = J_0 + ⌈J_0 − C_n⌋_{P_n} \\ \| = J_0 + ⌊C_n − J_0⌉_{P_n} \\ \| = J_0 + ((C_n − J_0) \bmod P_n) \end{split} \label{eq:nearestge} \end{equation}

For example, what is the first day at or after $$J_0 = 700$$ that corresponds to date $$\{1,0,2\}$$ in the calendar from the previous example?

We had $$C_n = 54$$. With formula \eqref{eq:nearestge} we then find $$J = 700 + ((54 − 700) \bmod 60) = 700 + ((−646) \bmod 60) = 700 + 14 = 714$$ which is correct: 714 − 60 = 654 is less than 700, so 714 is the first $$J$$ that is greater than or equal to $$J_0$$ and that fits date $$\{1,0,2\}$$.

We should find $$J = 714$$ for all $$J_0$$ from 714 − 60 + 1 = 655 through 714, and we do:

 $${J_0}$$ $${J_0 − C_n}$$ $${(C_n − J_0) \bmod P_n}$$ $${J}$$ 654 600 0 654 655 601 59 714 656 602 58 714 713 659 1 714 714 660 0 714 715 661 59 774

##### 12.10.4.3. The last before

The last $$J$$ before $$J_0$$ is one $$P_n$$ before the first $$J$$ at or after $$J_0$$, so

\begin{equation} J = J_0 − P_n − ((J_0 − C_n) \bmod P_n) \end{equation}

##### 12.10.4.4. The first after

The first $$J$$ after $$J_0$$ is one $$P_n$$ after the last $$J$$ at or before $$J_0$$, so

\begin{align} J \| = J_0 + P_n + ⌈J_0 − C_n⌋_{P_n} \\ \| = J_0 + P_n + ((C_n − J_0) \bmod P_n) \end{align}

### 12.11. Summary

This summary is in terms of days and months, but holds also for other units of time. We distinguish four different degrees of difficulty: from 1 to 4 the formulas get more complicated but also more capable.

1. The simplest calendar has two different month lengths, with the greatest being one greater than the smallest, and allows no shifting of the pattern of month lengths. Month 0 is always a short month, and month $$g − 1$$ (the last month before everything repeats itself) is always a long month.
2. Calendar type 2 is equal to type 1, except that shifting of the pattern of month lengths is allowed.
3. Calendar type 3 is equal to type 2, but now the difference between the month lengths can be more than one.
4. Calendar type 4 is equal to type 3, but allows more than two month lengths.

The following table summarizes the calendar formulas, and uses the following definitions:

• $$q$$ is the basic number of days in each month. For calendar types 1 through 3, $$q$$ is the number of days in short months. $$q$$ is a whole number greater than 0.
• $$p$$ is the average length of the infinite number of months that this calendar provides (even if in practice the calendar is used in a flat combination with another calendar so that only a limited number of months actually get used). $$p$$ is greater than zero, but is usually not a whole number.
• $$f$$, $$g$$ are the numerator and denominator from $$p = f/g$$. They are whole numbers greater than zero.
• $$ψ$$, $$ψ_i$$ is the fraction of the infinite number of months that has the non-basic month length (unequal to $$q$$).
• $$d$$, $$d_i$$ is the difference between a month length and the basic month length $$q$$. They are whole numbers. For calendar type 3, $$d$$ must be greater than zero. For calendar type 4 $$d_i$$ may be greater or less than zero.
• $$h$$, $$h_i$$ are the numerator and denominator from $$ψ = h/g$$ or $$ψ_i = h_i/g$$. They are whole numbers greater than zero.
• $$a$$, $$a_i$$ indicate by how many months the pattern of month lengths should be shifted compared to the simplest case. They are a whole numbers. Only the values from 0 through $$g − 1$$ are important: value $$a + g$$ has the same effect as value $$a$$.
• $$x$$ is the running month number. It is a whole number, and can be negative, zero, or positive.
• $$c$$ is the running day number of the beginning of month $$c$$. It is a whole number, and can be negative, zero, or positive.
• $$ζ$$ is a correction to be added to month number $$x$$ to get a better estimate for the month number. This is only relevant for calendar types 3 and 4.
• $$z$$ is the number of days since the beginning of the current month, $$z = y − c$$.

# 
$${p}$$ $${f}$$ $${c}$$ $${c'}$$ $${s}$$ $${ζ}$$
1 $${q+ψ}$$ $${qg+h}$$ $${\floorratio{fx}{g}}$$ $${\floorratio{gy+g−1}{f}}$$ $${0}$$ $${0}$$
2 $${q+ψ}$$ $${qg+h}$$ $${\floorratio{fx+s}{g}}$$ $${\floorratio{gy+g−s−1}{g}}$$ $${fa \bmod g}$$ $${0}$$
3 $${q+dψ}$$ $${qg+dh}$$ $${qx+d\floorratio{hx+s}{g}}$$ $${\floorratio{gy+dg−ds−1}{g}}$$ $${ha \bmod g}$$ $${\floorratio{z}{q + d}}$$
4 $${q+∑d_{i}ψ_i}$$ $${qg+∑d_{i}h_i}$$ $${qx+∑d_i\floorratio{h_{i}x+s_i}{g}}$$ $${\floorratio{gy+g∑_{\gt0}d_i−∑d_{i}s_i−1}{f}}$$ $${h_{i}a_i \bmod g}$$ $${\floorratio{z}{q + ∑_{\gt0}d_i}}$$

For calendar types 3 and 4, the formula for $$x$$ yields an upper boundary for the month number. The general procedure for finding the right month number is then:

Try the $$x$$ from the formula in the table. Calculate the corresponding $$c$$ and then $$z = y − c$$ and $$ζ$$. If $$ζ$$ is equal to 0, then you're done. Otherwise, add $$ζ$$ (which is negative) to $$x$$ and try again.

If $$d ≤ p$$ (for calendar type 3) or $$∑_{\gt0}d_i − ∑_{\lt0}d_i \lt p$$ (for calendar type 4), then you need to calculate $$ζ$$ at most once. Otherwise you may need to do it more often, up to $$⌈(d − 1)/p⌉ + 1$$ (for calendar type 3) or $$⌈(∑_{\gt0}d_i − ∑_{\lt0}d_i)/p⌉ + 1$$ (for calendar type 4) times.

If there are more that two relevant units of time, then different calendar levels must be combined. From calendar date in the direction of running day number we have

\begin{equation} y_i = c_i(x_i) + z_i \end{equation}

and from running day number in the direction of calendar date

\begin{align} x_i \| = c'_i(y_i) \\ z_i \| = y_i − c_i(x_i) \end{align}

If the smallest unit of time in two calendars is the same (for example, days), then those calendars should be combined in a flat manner, then $$z_{i+1} = y_i$$. If the smallest unit of time from calendar $$i + 1$$ is equal to the largest unit of time from calendar $$i$$, then those calendars should be combined in a stepped manner, then $$x_{i+1} = y_i$$.

## 13. Derivation for Specific Calendars

Almost all calendars distinguish between three basic periods: days, months, and years. One calendar level (straight line) links two periods, so at least two calendar levels are needed to link three periods.

### 13.1. The Julian Calendar

#### 13.1.1. From Julian Date to CJDN (1)

In the Julian calendar, every three regular years of 365 days are followed by a leap year of 366 days. This period of four years contains 3×365 + 366 = 1461 days. Each period of four years has the same sequence of months with their month lengths.

All months have 30 or 31 days, except for February which has 28 or 29 days. That extra-short month February is a bit of a problem; because of it, we have to deal not with two but with four different month lengths. We can work around the problem by regarding February as the last month of the calculation year. Then March is month 0 of the calculation year, and February is month 11 of the same calculation year.

For month $$m$$ of year $$j$$ we then find calculation month $$m_2$$ and calculation year $$j_2$$:

\begin{align} \{ c, m_2 \} \| = \Div(m − 3, 12) \\ c \| = \floorratio{m − 3}{12} \\ m_2 \| = m − 12c − 3 = (m ― 3) \bmod 12 \\ j_2 \| = j + c \end{align}

 $${m}$$ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $${c}$$ −1 −1 −1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 $${m_2}$$ 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 $${j_2}$$ $${j−1}$$ $${j−1}$$ $${j−1}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j}$$ $${j+1}$$ $${j+1}$$ $${j+1}$$

and in the opposite direction

\begin{align} \{ c, m − 1 \} \| = \Div(m_2 + 2, 12) \\ c \| = \floorratio{m_2 + 2}{12} \\ m \| = m_2 − 12c + 3 \\ j \| = j_2 + c \end{align}

 $${m_2}$$ 0 1 2 3 4 5 6 7 8 9 10 11 $${c}$$ 0 0 0 0 0 0 0 0 0 0 1 1 $${m}$$ 3 4 5 6 7 8 9 10 11 12 1 2 $${j}$$ $${j_2}$$ $${j_2}$$ $${j_2}$$ $${j_2}$$ $${j_2}$$ $${j_2}$$ $${j_2}$$ $${j_2}$$ $${j_2}$$ $${j_2}$$ $${j_2+1}$$ $${j_2+1}$$

If we use a flat combination of the calendar levels, then the lower calendar (days-months) starts over for every year in the upper calendar (days-years). The last month of the calculation year is then cut short by the upper calendar, so then it does not matter if that last month would contain too many days if it weren't cut short. Then we can give all calendar months 30 or 31 days, which makes the calculations a lot simpler.

The month lengths from March through January are 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31 - 30 - 31 - 31. We get this sequence of month lengths if we use calendar type 2 from section 12.11 with $$f = 153$$, $$g = 5$$, $$a = 4$$ so $$s = fa \bmod g = 153×4 \bmod 5 = 2$$. In that calendar, February gets 30 days, but that is cut short to 28 or 29 days by the upper calendar.

That upper calendar should yield 365 - 365 - 365 - 366 days and repeat that every 4 years. We get that sequence of year lengths if we use calendar type 1 with $$f = 1461$$, $$g = 4$$.

The first day of calculation year 0, i.e. 1 March of the year 0 in the Julian calendar, has CJDN $$J_0 = 1721118$$.

The steps to get from the Julian calendar to the CJDN are now:

1. Calculate calculation year number $$x_2$$, calculation month number $$x_1$$, and calculation day number $$z_1$$ from the year number $$j$$, month number $$m$$, and day number $$d$$. The first time period for each unit of time (calculation year, calculation month, calculation day) must have number 0, otherwise the calculations don't give the right results.

\begin{align} \{ c_0, x_1\} \| = \Div(m ― 2, 12) \\ c_0 \| = \floorratio{m − 3}{12} \\ x_1 \| = m − 12c_0 − 3 = (m ― 3) \bmod 12 \\ x_2 \| = j + c_0 \\ z_1 \| = d − 1 \end{align}

2. We combine the two calendar levels in the flat manner, so $$z_2 = y_1$$:

\begin{align} c_1 \| = \floorratio{153x_1 + 2}{5} \\ y_1 \| = c_1 + z_1 \\ z_2 \| = y_1 \\ c_2 \| = \floorratio{1461x_2}{4} \\ y_2 \| = c_2 + z_2 \end{align}

3. Add the CJDN of running day number 0:

\begin{equation} J = y_2 + J_0 = y_2 + 1721118 \end{equation}

For example, what CJDN corresponds to Julian date 6 July 2003? Then $$j = 2003$$, $$m = 7$$, $$d = 6$$, and thus

\begin{align*} c_0 \| = \floorratio{7 − 3}{12} = 0 \\ x_1 \| = 7 − 12×0 − 3 = 4 \\ x_2 \| = 2003 + 0 = 2003 \\ z_1 \| = 6 − 1 = 5 \\ c_1 \| = \floorratio{153×4 + 2}{5} = \floorratio{614}{5} = 122 \\ y_1 \| = 122 + 5 = 127 \\ z_2 \| = 127 \\ c_2 \| = \floorratio{1461×2003}{4} = \floorratio{2926383}{4} = 731595 \\ y_2 \| = 731595 + 127 = 731722 \\ J \| = 731722 + 1721118 = 2452840 \end{align*}

Another example. What CJDN corresponds to Julian date 1 December 2000? Then $$j = 2000$$, $$m = 12$$, $$d = 1$$, so

\begin{align*} c_0 \| = \floorratio{12 − 3}{12} = 0 \\ x_1 \| = 12 − 12×0 − 3 = 9 \\ x_2 \| = 2000 + 0 = 2000 \\ z_1 \| = 1 − 1 = 0 \\ c_1 \| = \floorratio{153×9 + 2}{5} = \floorratio{1379}{5} = 275 \\ y_1 \| = 275 + 0 = 275 \\ z_2 \| = 275 \\ c_2 \| = \floorratio{1461×2000}{4} = \floorratio{2922000}{4} = 730500 \\ y_2 \| = 730500 + 275 = 730775 \\ J \| = 730775 + 1721118 = 2451893 \end{align*}

We can combine and squeeze these steps into:

\begin{align} c_0 \| = \floorratio{m − 3}{12} \\ J \| = \floorratio{1461×(j + c_0)}{4} + \floorratio{153m − 1836c_0 − 457}{5} + d + 1721117 \end{align}

For the same example as above we find $$j = 2003$$, $$m = 7$$, $$d = 6$$, so

\begin{align*} c_0 \| = \floorratio{7 − 3}{12} = 0 \\ J \| = \floorratio{1461×(2003 + 0)}{4} + \floorratio{153×7 − 1836×0 − 457}{5} + 6 + 1721117 \\ \| = \floorratio{2926383}{4} + \floorratio{614}{5} + 6 + 1721117 \\ \| = 731595 + 122 + 6 + 1721117 = 2452840 \end{align*}

#### 13.1.2. From CJDN to Julian Date (1)

We run through the opposite procedure from the one described above, with the same calendar levels as above. Calendar level 1 is of calendar type 2 with $$f = 153$$, $$g = 5$$, $$a = 4$$ (so $$s = fa \bmod g = 153×4 \bmod 5 = 2$$), and calendar level 2 is of calendar type 1 with $$f = 1461$$, $$g = 4$$.

First we calculate the running day number from the Julian Day Number, by subtracting the Julian Day Number of running day number 0:

\begin{equation} y_2 = J − J_0 = J − 1721118 \end{equation}

Then we calculate the calculation year, calculation month, and calculation day from the running day number, in the flat manner:

\begin{align} \{x_2, r_2\} \| = \Div(4y_2 + 3, 1461) \\ z_2 \| = \floorratio{r_2}{4} \\ \{x_1, r_1\} \| = \Div(5z_2 + 2, 153) \\ z_1 \| = \floorratio{r_1}{5} \end{align}

And lastly we translate these calculation values to calendar values:

\begin{align} c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = x_2 + c_0 \\ m \| = x_1 − 12c_0 + 3 = ((x_1 + 2) \bmod 12) + 1 \\ d \| = z_1 + 1 \end{align}

For example, which Julian Date corresponds to Julian Day number 2452840? Then $$J = 2452840$$ so

\begin{align*} y_2 \| = 2452840 − 1721118 = 731722 \\ \{x_2, r_2\} \| = \Div(4×731722 + 3, 1461) = \Div(2926891, 1461) = \{2003, 508\} \\ z_2 \| = \floorratio{508}{4} = 127 \\ y_1 \| = 127 \\ \{x_1, r_1\} \| = \Div(5×127 + 2, 153) = \Div(637, 153) = \{4, 25\} \\ z_1 \| = \floorratio{25}{5} = 5 \\ c_0 \| = \floorratio{4 + 2}{12} = 0 \\ j \| = 2003 + 0 = 2003 \\ m \| = 4 − 12×0 + 3 = 7 \\ d \| = 5 + 1 = 6 \end{align*}

The date is July 6th, 2003.

Which Julian date corresponds to CJDN 2451893? Then $$J = 2451893$$ so

\begin{align*} y_2 \| = 2451893 − 1721118 = 730775 \\ \{x_2, r_2\} \| = \Div(4×730775, 1461) = \Div(2923103, 1461) = \{2000, 1103\} \\ z_2 \| = \floorratio{1103}{4} = 275 \\ y_1 \| = z_2 = 275 \\ \{x_1, r_1\} \| = \Div(5×275 + 2, 153) = \Div(1377, 153) = \{9, 0\} \\ z_1 \| = \floorratio{0}{4} = 0 \\ c_0 \| = \floorratio{9 + 2}{12} = \floorratio{11}{12} = 0 \\ j \| = 2000 + 0 = 2000 \\ m \| = 9 − 12×0 + 3 = 12 \\ d \| = 0 + 1 = 1 \end{align*}

The date is 1 December 2000.

And which Julian date corresponds to CJDN 173? Then $$J = 173$$ and

\begin{align*} y_2 \| = 173 − 1721118 = −1720945 \\ \{x_2, r_2\} \| = \Div(4×−1720945, 1461) = \Div(−6883777, 1461) = \{−4712, 455\} \\ z_2 \| = \floorratio{455}{4} = 113 \\ y_1 \| = 113 \\ \{x_1, r_1\} \| = \Div(5×113 + 2, 153) = \Div(567, 153) = \{3, 108\} \\ z_1 \| = \floorratio{108}{5} = 21 \\ c_0 \| = \floorratio{3 + 2}{12} = 0 \\ j \| = −4712 + 0 = −4712 \\ m \| = 3 − 12×0 + 3 = 6 \\ d \| = 21 + 1 = 22 \end{align*}

The date is 22 June −4712.

We can combine and compress these a bit, to

\begin{align} y_2 \| = J − 1721118 \\ k_2 \| = 4y_2 + 3 \\ k_1 \| = 5\floorratio{k_2 \bmod 1461}{4} + 2 \\ x_1 \| = \floorratio{k_1}{153} \\ c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = \floorratio{k_2}{1461} + c_0 \\ m \| = x_1 − 12c_0 + 3 \\ d \| = \floorratio{k_1 \bmod 153}{5} + 1 \end{align}

or, with use of $$\Div()$$,

\begin{align} \{x_2, r_2\} \| = \Div(4J − 6884469, 1461) \\ \{x_1, r_1\} \| = \Div\left( 5\floorratio{r_2}{4} + 2, 153 \right) \\ d \| = \floorratio{r_1}{5} + 1 \\ c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = x_2 + c_0 \\ m \| = x_1 − 12c_0 + 3 \end{align}

Which Julian date corresponds to CJDN 2451893? Then $$J = 2451893$$ so

\begin{align*} y_2 \| = 2451893 − 1721118 = 730775 \\ k_2 \| = 4×730775 + 3 = 2923103 \\ k_1 \| = 5\floorratio{2923103 \bmod 1461}{4} + 2 = 5\floorratio{1103}{4} + 2 = 5×275 + 2 = 1377 \\ x_1 \| = \floorratio{1377}{153} = 9 \\ c_0 \| = \floorratio{9 + 2}{12} = \floorratio{11}{12} = 0 \\ j \| = \floorratio{2923103}{1461} + 0 = 2000 + 0 = 2000 \\ m \| = 9 − 12×0 + 3 = 12 \\ d \| = \floorratio{(1377 \bmod 153}{5} + 1 = \floorratio{0}{5} + 1 = 1 \end{align*}

or

\begin{align} \{x_2, r_2\} \| = \Div(4×2451893 − 6884469, 1461) = \Div(2923103, 1461) = \{2000, 1103\} \\ \{x_1, r_1\} \| = \Div\left( 5\floorratio{1103}{4} + 2, 153 \right) = \Div(5×275 + 2, 153) = \Div(1377, 153) = \{9, 0\} \\ d \| = \floorratio{0}{5} + 1 = 1 \\ c_0 \| = \floorratio{9 + 2}{12} = \floorratio{11}{12} = 0 \\ j \| = 2000 + 0 = 2000 \\ m \| = 9 − 12×0 + 3 = 12 \end{align}

The date is 1 December 2000.

#### 13.1.3. With the Standard Algorithms

With the standard algorithms for calendar levels we can describe this conversion as follows:

$${q}$$ $${g}$$ $${d}$$ $${h}$$ $${s}$$ $${x}$$ $${z}$$
⇒ 
$${y}$$ description
1 1 1 0 −1 $${m}$$ 0
⇒ 
$${y_1}$$ month index (0…11)
12 1 1 0 0 $${j}$$ $${y_1}$$
⇒ 
$${y_2}$$ running month index
1 1 1 0 −2 $${y_2}$$ 0
⇒ 
$${y_3}$$ running calculation month index
12 1 1 0 0 $${y_4}$$ $${z_4}$$
⇐ 
$${y_3}$$ calculation year, calculation month
1 1 1 0 −1 $${d}$$ 0
⇒ 
$${y_5}$$ calculation day of the month
30 5 1 3 2 $${z_4}$$ $${y_5}$$
⇒ 
$${y_6}$$ running day number in the year
365 4 1 1 0 $${y_4}$$ $${y_6}$$
⇒ 
$${y_7}$$ running day number
1 1 1 0 1721118 $${y_7}$$ 0
⇒ 
$${J}$$ CJDN

If you transform from date to CJDN then you go from top to bottom and follow the arrows, and if you transform frm CJDN to date then you go from bottom to top and against the arrows.

For $$j = 2003, m = 7, d = 6$$ we find

$${q}$$ $${g}$$ $${d}$$ $${h}$$ $${s}$$ $${x}$$ $${z}$$
↔ 
$${y}$$ description
1 1 1 0 −1 7 0
→ 
$${y_1}$$ 6 month index (0…11)
12 1 1 0 0 2003 6
→ 
$${y_2}$$ 24042 running month index
1 1 1 0 −2 24042 0
→ 
$${y_3}$$ 24040 running calculation month index
12 1 1 0 0 $${y_4}$$ 2003 $${z_4}$$ 4
← 
24040 calculation year, calculation month
1 1 1 0 −1 6 0
→ 
$${y_5}$$ 5 calculation day of the month
30 5 1 3 2 4 5
→ 
$${y_6}$$ 127 running day number in the year
365 4 1 1 0 2003 127
→ 
$${y_7}$$ 731722 running day number
1 1 1 0 1721118 731722 0
→ 
$${J}$$ 2452840 CJDN

#### 13.1.4. From Julian Date to CJDN (2)

We can also use a stepped combination of calendars; then we need a calendar level between a running month number $$x$$ and a running day number $$y$$, taking into account leap years. In that case there is no longer a higher calendar level that will shorten a too-long last month, so the formulas must be correct also for very large running month numbers.

First we calculate the running month number $$y_1$$ from the year $$x_1$$ and the month number $$z_1$$ within the year. The formula is very simple:

\begin{equation} y_1 = 12x_1 + z_1 \end{equation}

At the second calendar level, we must calculate a running day number from the running month number. Of the 12 months of a year, 7 are long, with 31 days each, so we need a formula $$⌊7×(x_2 + a)/12⌋$$ (which repeats itself every 12 months), but what should be the value of $$a$$?

If $$a = 0$$, then $$⌊7x_2/12⌋$$ yields the values (for $$x_2$$ from 0 through 12): 0 0 1 1 2 2 3 4 4 5 5 6 7. Each time that the value increases by one, the preceding month was a long one, so these values mean that the sequence of short (S) and long (L) months was: S L S L S L L S L S L L, but we're searching for L S L S L S L L S L S L. We get that if we shift the pattern by −1 month (one to the left), so $$a = −1$$. Then $$s = fa \bmod g = −7 \bmod 12 = 5$$, so we find $$⌊(7x_2 + 5)/12⌋$$.

Then all months are good, except for February ($$x_2 = 1$$), because that month gets 30 days but should have only 28 days in a regular year or 29 days in a leap year. We subtract 2 days from every February using the formula $$−2⌊(x_2 + 10)/12⌋$$. We add 1 day back in for the first of every four years by using $$⌊(x_2 + 46)/48⌋$$. All in all we find

\begin{equation} c_2 = 30x_2 + \floorratio{7x_2 + 5}{12} − 2\floorratio{x_2 + 10}{12} + \floorratio{x_2 + 46}{48} \end{equation}

This is calendar type 4, but for calculating in the opposite direction it is better if all fractions use the same numerator. The least common multiple of the numerators 12, 12, and 48 is 48, so we use $$g = 48$$. Then

\begin{equation} c_2 = 30x_2 + \floorratio{28x_2 + 20}{48} − 2\floorratio{4x_2 + 40}{48} + \floorratio{x_2 + 46}{48} \end{equation}

which means calendar type 4 with $$q = 30$$, $$g = 48$$, $$\{d_1,h_1,s_1\} = \{1,28,20\}$$, $$\{d_2,h_2,s_2\} = \{−2,4,40\}$$, $$\{d_3,h_3,s_3\} = \{1,1,46\}$$.

Then the procedure is

1. Translate the year $$j$$, month $$m$$, day $$d$$ to calculation year $$x_1$$, calculation month $$z_1$$, and calculation day $$z_2$$. The number of the first calculation year/month/day must be 0, otherwise the calculations don't give the right results.

\begin{align} x_1 \| = j \\ z_1 \| = m − 1 \\ z_2 \| = d − 1 \end{align}

The CJDN $$J_0$$ that corresponds to $$\{x_1,z_1,z_2\} = \{0,0,0\}$$, i.e., 1 January of the year 0 (in the Julian calendar) is 1721058.

2. Combine the calendar levels in the stepped fashion, i.e., $$x_2 = y_1$$:

\begin{align} c_1 \| = 12x_1 \\ y_1 \| = c_1 + z_1 \\ x_2 \| = y_1 \\ c_2 \| = 30x_2 + \floorratio{28x_2 + 20}{48} − 2\floorratio{4x_2 + 40}{48} + \floorratio{x_2 + 46}{48} \\ y_2 \| = c_2 + z_2 \end{align}

3. Add the CJDN of running day number 0:

\begin{equation} J = y_2 + J_0 = y_2 + 1721058 \end{equation}

For example, what CJDN corresponds to Julian date July 6th, 2003? Then $$j = 2003$$, $$m = 7$$, $$d = 6$$, so $$x_1 = 2003$$, $$z_1 = 6$$, $$z_2 = 5$$. Then we find

\begin{align*} c_1 \| = 12×2003 = 24036 \\ y_1 \| = 24036 + 6 = 24042 \\ x_2 \| = 24042 \\ c_2 \| = 30×24042 + \floorratio{28×24042 + 20}{48} − 2×\floorratio{4×24042 + 40}{48} + \floorratio{24042 + 46}{48} \\ \| = 721260 + \floorratio{673196}{48} − 2×\floorratio{96208}{48} + \floorratio{24088}{48} \\ \| = 721260 + 14024 − 2×2004 + 501 = 731777 \\ y_2 \| = 731777 + 5 = 731782 \\ J \| = 731782 + 1721058 = 2452840 \end{align*}

We can combine the above formulas to

\begin{align} c \| = 12j + m \\ J \| = 30c + \floorratio{7c − 2}{12} − 2\floorratio{c + 9}{12} + \floorratio{c + 45}{48} + d + 1721027 \end{align}

For example, what CJDN corresponds to Julian date July 6th, 2003? Then $$j = 2003$$, $$m = 7$$, $$d = 6$$, so

\begin{align*} c \| = 12×2003 + 7 = 24043 \\ J \| = 30×24043 + \floorratio{7×24043 − 2}{12} − 2\floorratio{24043 + 9}{12} + \floorratio{24043 + 45}{48} + 6 + 1721027 \\ \| = 721290 + \floorratio{168299}{12} − 2\floorratio{24052}{12} + \floorratio{24088}{48} + 6 + 1721027 \\ \| = 721290 + 14024 − 2×2004 + 501 + 6 + 1721027 = 2452840 \end{align*}

#### 13.1.5. From Julian Day Number to Julian Date (2)

We run through the above procedure in the opposite direction.

1. Calculate the running day number $$y_2$$ by subtracting the CJDN of running day number 0 from the Julian Day number $$J$$:

\begin{equation} y_2 = J − J_0 = J − 1721058 \end{equation}

2. With $$q = 30$$, $$g = 48$$, $$\{d_1,h_1,s_1\} = \{1,28,20\}$$, $$\{d_2,h_2,s_2\} = \{−2,4,40\}$$, $$\{d_3,h_3,s_3\} = \{1,1,46\}$$ we have $$f = qg + ∑d_{i}h_i = 30×48 + 1×28 − 2×4 + 1×1 = 1461$$, so the average month length is $$f/g = 1461/48 = 487/16 = 30 + 7/16$$ days. Also $$∑d_{i}s_i = 1×20 − 2×40 + 1×46 = −14$$ and $$∑_{\gt0}d_i = 2$$ and so

\begin{equation} g∑_{\gt0}d_i − ∑_i d_i s_i − 1 = 48×2 − (−14) − 1 = 109 \end{equation}

Translate to calculation year $$x_1$$, calculation month $$z_1$$, and calculation day $$z_2$$:

\begin{align} x_2 \| = c'_2(y_2) = \floorratio{48y_2 + 109}{1461} \\ \| \begin{split} c_2 \| = 30x_2 + \floorratio{28x_2 + 20}{48} − 2\floorratio{4x_2 + 40}{48} + \floorratio{x_2 + 46}{48} \\ \| = 30x_2 + \floorratio{7x_2 + 5}{12} − 2\floorratio{x_2 + 10}{12} + \floorratio{x_2 + 46}{48} \end{split} \\ z_2 \| = y_2 − c_2 \\ ζ \| = \floorratio{z_2}{32} \\ x_2 \| ↤ x_2 + ζ \\ \| \begin{split} c_2 \| ↤ 30x_2 + \floorratio{28x_2 + 20}{48} − 2\floorratio{4x_2 + 40}{48} + \floorratio{x_2 + 46}{48} \\ \| = 30x_2 + \floorratio{7x_2 + 5}{12} − 2\floorratio{x_2 + 10}{12} + \floorratio{x_2 + 46}{48} \end{split} \\ z_2 \| ↤ y_2 − c_2 \\ y_1 \| = x_2 \\ x_1 \| = c'_1(y_1) = \floorratio{y_1}{12} \\ c_1 \| = 12x_1 \\ z_1 \| = y_1 − c_1 \end{align}

3. Then translate to calendar year $$j$$, calendar month $$m$$, and calendar day $$d$$:

\begin{align} j \| = x_1 \\ m \| = z_1 + 1 \\ d \| = z_2 + 1 \end{align}

For example, what Julian Date corresponds to CJDN 2452840? Then $$J = 2452840$$, so

\begin{align*} y_2 \| = 2452840 − 1721058 = 731782 \\ x_2 \| = \floorratio{48×731782 + 81}{1461} = \floorratio{35125617}{1461} = 24042 \\ c_2 \| = 30×24042 + \floorratio{28×24042 + 20}{48} − 2×\floorratio{4×24042 + 40}{48} + \floorratio{24042 + 46}{48} \\ \| = 721260 + \floorratio{673196}{48} − 2×\floorratio{96208}{48} + \floorratio{24088}{48} \\ \| = 721260 + 14024 − 4008 + 501 = 731777 \\ z_2 \| = 731782 − 731777 = 5 \\ ζ \| = \floorratio{5}{32} = 0 \end{align*}

Because $$ζ = 0$$, the $$x_2$$, $$c_2$$, $$z_2$$ remain unchanged.

\begin{align*} y_1 \| = 24042 \\ x_1 \| = \floorratio{24042}{12} = 2003 \\ c_1 \| = 12×2003 = 24036 \\ z_1 \| = 24042 − 24036 = 6 \\ j \| = 2003 \\ m \| = 6 + 1 = 7 \\ d \| = 5 + 1 = 6 \end{align*}

so the date is July 6th, 2003.

### 13.2. The GregorianCalendar

#### 13.2.1. From Gregorian Date to CJDN (1)

The Gregorian calendar is equal to the Julian calendar, except for the rules that say when the month of February has 29 days instead of 28 days. In the Julian calendar, February has 29 days in every fourth year, when the year number is evenly divisible by 4. In the Gregorian calendar the same rule holds, except that February has 28 days when the year number is evenly divisible by 100, except if the year number is evenly divisible by 400, when February has 29 days after all.

To approximate Gregorian years with a straight line with a single pattern, the leap values have to return after every $$⌊Q⌋$$ or $$⌈Q⌉$$ periods (for a well-chosen $$Q$$; see equation \eqref{eq:Q}). Unfortunately, the leap year rules of the Gregorian Calendar do not meet that demand, because they mean that the time between two leap years is sometimes 4 years and sometimes 8 years.

The following $$p$$ might be considered:

• 146097 days in 400 years → $$p = f/g = 146097/400$$. This yields 85 sequences of a leap year every 4 years, but also 12 sequences of a leap year every 5 years. That does not fit the Gregorian Calendar, so this straight line is not useful for calculating the Gregorian Calendar.
• 146097 days in 100 periods of 4 years → $$p = f/g = 146097/100$$. This yields two sequences of 33 periods (of 4 years) with one fewer leap day at the end (1460 days instead of 1461 days), and one sequence of 34 periods. The Gregorian Calendar has two sequences of 25 periods (of 4 years) with one fewer leap day at the end, and then a sequence of 50 periods with one fewer leap day at the end. This one is not suitable, either.
• 36524 days in 100 years → $$p = f/g = 36524/100$$. This yields 20 sequences of a leap year every 4 years, but also 4 sequences of a leap year every 5 years. This one is not suitable.
• 146097 days in 4 periods of 100 years → $$p = f/g = 146097/4$$. This yields 3 sequences of 36524 days and 1 sequence of 36525 days, and that fits the Gregorian Calendar.
• 36525 days in 100 years → $$p = f/g = 36525/100$$. This yields 25 sequences with a leap year every 4 years. That fits, except that we sometimes need to stop already after 36524 days.

We can now use the 146097/4 line to find the 100-year periods, and then use the 36525/100 line to find the years within the 100-year period, and finally (just like for the Julian Calendar) use the 153/5 line to find the months within the year. 1 March of year 0 in the Gregorian Calendar corresponds to $$J_0 = 1721120$$. We then use three calendar levels, with the following values:

$${i}$$ $${f_i}$$ $${g_i}$$ $${a_i}$$ $${s_i}$$
1 153 5 4 2
2 36525 100 0 0
3 146097 4 0 0

The steps to get from the Gregorian calendar to the CJDN are now:

1. Calculate calculation century number $$x_3$$, calculation year number $$x_2$$, calculation month number $$x_1$$, and calculation day number $$z_1$$ from the year number $$j$$, month number $$m$$, and day number $$d$$

\begin{align} c_0 \| = \floorratio{m − 3}{12} \\ x_4 \| = j + c_0 \\ \{x_3, x_2\} \| = \Div(x_4, 100) \\ x_1 \| = m − 12c_0 − 3 \\ z_1 \| = d − 1 \end{align}

2. We combine the two calendar levels in the flat manner, so $$z_2 = y_1$$ and $$z_3 = y_2$$:

\begin{align} c_1 \| = \floorratio{153x_1 + 2}{5} \\ y_1 \| = c_1 + z_1 \\ z_2 \| = y_1 \\ c_2 \| = \floorratio{36525x_2}{100} \\ y_2 \| = c_2 + z_2 \\ z_3 \| = y_2 \\ c_3 \| = \floorratio{146097x_3}{4} \\ y_3 \| = c_3 + z_3 \end{align}

3. Add the CJDN of running day number 0:

\begin{equation} J = y_3 + J_0 = y_3 + 1721120 \end{equation}

Combined and condensed, this yields:

\begin{align} c_0 \| = \floorratio{m − 3}{12} \\ x_4 \| = j + c_0 \\ \{x_3, x_2\} \| = \Div(x_4, 100) \\ x_1 \| = m − 12c_0 − 3 \\ J \| = \floorratio{146097x_3}{4} + \floorratio{36525x_2}{100} + \floorratio{153x_1 + 2}{5} + d + 1721119 \label{eq:gregtocjdn1} \end{align}

For example, which Julian Day number corresponds to Gregorian date July 6th, 2003? Then $$j = 2003$$, $$m = 7$$, $$d = 6$$ and then

\begin{align*} c_0 \| = \floorratio{7 − 3}{12} = 0 \\ x_4 \| = 2003 + 0 = 2003 \\ \{x_3, x_2\} \| = \Div(2003, 100) = \{20, 3\} \\ x_1 \| = 7 − 12×0 − 3 = 4 \\ J \| = \floorratio{146097×20}{4} + \floorratio{36525×3}{100} + \floorratio{153×4 + 2}{5} + 6 + 1721119 \\ \| = 730485 + 1095 + 122 + 6 + 1721119 = 2452827 \end{align*}

#### 13.2.2. From CJDN to Gregorian Date (1)

Now we go in the opposite direction again, with the same three calendar levels as before.

First we subtract the CJDN of running day number 0 from the CJDN to get the running day number:

\begin{equation} y_3 = J − J_0 = J − 1721120 \end{equation}

Then we translate the running day number in the flat manner into a calculation century, calculation year, calculation month, and calculation day:

\begin{align} \{x_3, r_3\} \| = \Div(4y_3 + 3, 146097) \\ z_3 \| = \floorratio{r_3}{4} \\ y_2 \| = z_3 \\ \{x_2, r_2\} \| = \Div(100y_2 + 99, 36525) \\ z_2 \| = \floorratio{r_2}{100} \\ y_1 \| = z_2 \\ \{x_1, r_1\} \| = \Div(5y_1 + 2, 153) \\ z_1 \| = \floorratio{r_1}{5} \end{align}

And lastly we translate to day-month-year:

\begin{align} c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = 100x_3 + x_2 + c_0 \\ m \| = x_1 − 12c_0 + 3 \\ d \| = z_1 + 1 \end{align}

For example, which date in the Gregorian calendar corresponds to CJDN 2452827? Then $$J = 2452827$$ and then

\begin{align*} y_3 \| = J − 1721120 = 731707 \\ \{x_3, r_3\} \| = \Div(4×731707, 146097) = \Div(2926831, 146097) = \{20, 4891\} \\ z_3 \| = \floorratio{4891}{4} = 1222 \\ y_2 \| = 1222 \\ \{x_2, r_2\} \| = \Div(100×1222 + 99, 36525) = \Div(122299, 36525) = \{3, 12724\} \\ z_2 \| = \floorratio{12724}{100} = 127 \\ y_1 \| = 127 \\ \{x_1, r_1\} \| = \Div(5×127 + 2, 153) = \Div(637, 153) = \{4, 25\} \\ z_1 \| = \floorratio{25}{5} = 5 \\ c_0 \| = \floorratio{4 + 2}{12} = 0 \\ j \| = 100×20 + 3 + 0 = 2003 \\ m \| = 4 − 12×0 + 3 = 7 \\ d \| = 5 + 1 = 6 \end{align*}

The date is July 6th, 2003.

This condenses a little bit, to:

\begin{align} \{x_3, r_3\} \| = \Div(4J − 6884477, 146097) \\ \{x_2, r_2\} \| = \Div\left( 100\floorratio{r_3}{4} + 99, 36525 \right) \\ \{x_1, r_1\} \| = \Div\left( 5\floorratio{r_2}{100} + 2, 153 \right) \\ d \| = \floorratio{r_1}{5} + 1 \\ c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = 100x_3 + x_2 + c_0 \\ m \| = x_1 − 12c_0 + 3 \end{align}

For example, which date in the Gregorian calendar corresponds to CJDN 2452827? Then $$J = 2452827$$ and then

\begin{align*} \{x_3, r_3\} \| = \Div(4×2452827 − 6884477, 146097) = \Div(2926831, 146097) = \{20, 4891\} \\ \{x_2, r_2\} \| = \Div\left( 100\floorratio{4891}{4} + 99, 36525 \right) = \Div(100×1222 + 99, 36525) = \Div(122299, 36525) = \{3, 12724\} \\ \{x_1, r_1\} \| = \Div\left( 5\floorratio{12724}{100} + 2, 153 \right) = \Div(5×127 + 2, 153) = \Div(637, 153) = \{4, 25\} \\ d \| = \floorratio{25}{5} + 1 = 5 + 1 = 6 \\ c_0 \| = \floorratio{4 + 2}{12} = 0 \\ j \| = 100×20 + 3 + 0 = 2003 \\ m \| = 4 − 12×0 + 3 = 7 \end{align*}

The date is July 6th, 2003.

#### 13.2.3. From Gregorian Date to CJDN (2)

We can make do with just two calendar levels combined in the flat manner if we use calendar type 4 for the upper level, with multiple month lengths. Then, for that upper level,

\begin{align*} q \| = 365 \\ g \| = 400 \\ \{d_1,h_1,s_1\} \| = \{1,100,0\} \\ \{d_2,h_2,s_2\} \| = \{−1,4,0\} \\ \{d_3,h_3,s_3\} \| = \{1,1,0\} \end{align*}

The lower calendar level is the same as before, so

\begin{align} c_3 \| = \floorratio{m − 3}{12} \\ x_2 \| = j + c_3 \\ x_1 \| = m − 12c_3 − 3 \\ z_1 \| = d − 1 \\ c_1 \| = \floorratio{153x_1 + 2}{5} \\ y_1 \| = c_1 + z_1 \\ z_2 \| = y_1 \\ \| \begin{split} c_2 \| = 365x_2 + \floorratio{100x_2}{400} − \floorratio{4x_2}{400} + \floorratio{x_2}{400} \\ \| = 365x_2 + \floorratio{x_2}{4} − \floorratio{x_2}{100} + \floorratio{x_2}{400} \end{split} \\ y_2 \| = c_2 + z_2 \\ J \| = y_2 + J_0 = y_2 + 1721120 \end{align}

For example, which CJDN corresponds to Gregorian date July 6th, 2003? Then $$j = 2003$$, $$m = 7$$, $$d = 6$$, so

\begin{align*} c_3 \| = \floorratio{7 − 3}{12} = 0 \\ x_1 \| = 7 − 12×0 − 3 = 4 \\ x_2 \| = 2003 + 0 = 2003 \\ z_1 \| = 6 − 1 = 5 \\ c_1 \| = \floorratio{153×4 + 2}{5} = \floorratio{614}{5} = 122 \\ y_1 \| = 122 + 5 = 127 \\ z_2 \| = 127 \\ c_2 \| = 365×2003 + \floorratio{100×2003}{400} − \floorratio{4×2003}{400} + \floorratio{2003}{400} \\ \| = 731095 + \floorratio{200300}{400} − \floorratio{8012}{400} + 5 \\ \| = 731095 + 500 − 20 + 5 = 731580 \\ y_2 \| = 731580 + 127 = 731707 \\ J \| = 731707 + 1721120 = 2452827 \end{align*}

This condenses to:

\begin{align} c_3 \| = \floorratio{m − 3}{12} \\ x_2 \| = j + c_3 \\ x_1 \| = m − 12c_3 − 3 \\ J \| = 365x_2 + \floorratio{x_2}{4} − \floorratio{x_2}{100} + \floorratio{x_2}{400} + \floorratio{153x_1 + 2}{5} + d + 1721119 \end{align}

#### 13.2.4. From CJDN to Gregorian Date (2)

With the upper calendar of type 4, we go in the opposite direction, again with

$$q = 365$$, $$g = 400$$, $$\{d_1,h_1,s_1\} = \{1,100,0\}$$, $$\{d_2,h_2,s_2\} = \{−1,4,0\}$$, $$\{d_3,h_3,s_3\} = \{1,1,0\}$$

Then

\begin{equation} f = qg + ∑d_{i}h_i = 365×400 + 1×100 − 1×4 + 1×1 = 146097 \end{equation}

so the average length of the year is $$f/g = 146097/400 = 365 \frac{97}{400}$$ days.

First we calculate the running day number from the CJDN, by subtracting the CJDN of running day number 0:

\begin{equation} y_2 = J − J_0 = J − 1721120 \end{equation}

Then we calculate the calculation year, calculation month, and calculation day from the running day number, in the flat manner:

\begin{align} x_2 \| = c'_2(y_2) = \floorratio{400y_2 + 799}{146097} \\ \| \begin{split} c_2 \| = 365x_2 + \floorratio{100x_2}{400} − \floorratio{4x_2}{400} + \floorratio{x_2}{400} \\ \| = 365x_2 + \floorratio{x_2}{4} − \floorratio{x_2}{100} + \floorratio{x_2}{400} \end{split} \\ z_2 \| = y_2 − c_2 \\ ζ \| = \floorratio{z_2}{367} \\ x_2 \| ↤ x_2 + ζ \\ \| \begin{split} c_2 \| ↤ 365x_2 + \floorratio{100x_2}{400} − \floorratio{4x_2}{400} + \floorratio{x_2}{400} \\ \| = 365x_2 + \floorratio{x_2}{4} − \floorratio{x_2}{100} + \floorratio{x_2}{400} \end{split} \\ z_2 \| ↤ y_2 − c_2 \\ y_1 \| = z_2 \\ x_1 \| = c'_1(y_1) = \floorratio{5y_1 + 2}{153} \\ c_1 \| = \floorratio{153x_1 + 2}{5} \\ z_1 \| = y_1 − c_1 \end{align}

And lastly we translate these calculation values to calendar values:

\begin{align} c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = x_2 + c_0 \\ m \| = x_1 − 12c_0 + 3 \\ d \| = z_1 + 1 \end{align}

For example, which date in the Gregorian calendar corresponds to CJDN 2452827? Then $$J = 2452827$$ and then

\begin{align*} y_2 \| = J − 1721120 = 731707 \\ x_2 \| = \floorratio{400×731707 + 799}{146097} = \floorratio{292683599}{146097} = 2003 \\ c_2 \| = 365×2003 + \floorratio{100×2003}{400} − \floorratio{4×2003}{400} + \floorratio{2003}{400} \\ \| = 731095 + \floorratio{200300}{400} − \floorratio{8012}{400} + 5 = 731095 + 500 − 20 + 5 = 731580 \\ z_2 \| = 731707 − 731580 = 127 \\ ζ \| = \floorratio{127}{367} = 0 \end{align*}

$$ζ = 0$$, so $$x_2$$, $$c_2$$, $$z_2$$ remain the same. Then

\begin{align*} y_1 \| = 127 \\ x_1 \| = \floorratio{5×127 + 2}{153} = \floorratio{637}{153} = 4 \\ c_1 \| = \floorratio{153×4 + 2}{5} = \floorratio{614}{5} = 122 \\ z_1 \| = 127 − 122 = 5 \\ c_0 \| = \floorratio{4 + 2}{12} = 0 \\ j \| = 2003 + 0 = 2003 \\ m \| = 4 − 12×0 + 3 = 7 \\ d \| = 5 + 1 = 6 \end{align*}

The date is July 6th, 2003.

The numerator of $$x_2$$ can get very large in this algorithm: In the example it was equal to 292,683,599 for a date in the year 2003. If you use this algorithm in a computer program that uses values of 32 bits large, then $$x_2$$ will get too large for dates more than 14,698 years from Julian Day number 0, i.e., from the year 9986. Section 12.2 explains how to find a solution for this. Such a solution is to replace formula

$x_2 = \floorratio{400y_2 + 799}{146097}$

with

\begin{align} q \| = \floorratio{y_2}{1461} \\ r \| = y_2 \bmod 1461 \\ x_2 \| = 4q + \floorratio{12q + 400r + 799}{146097} \end{align}

For example, when $$y_2 = 9,000,000$$, then

$x_2 = \floorratio{400×9000000 + 799}{146097} = \floorratio{3600000799}{146097} = 24641$

The greatest intermediate result is 3,600,000,799, which exceeds the greatest value (2,147,483,648) that fits a 32-bit value. With the alternative, we find

\begin{align*} q \| = \floorratio{9000000}{1461} = 6160 \\ r \| = 9000000 \bmod 1461 = 240 \\ x_2 \| = 4×6160 + \floorratio{12×6160 + 400×240 + 799}{146097} = 24640 + \floorratio{170719}{146097} = 24640 + 1 = 24641 \end{align*}

Now the greatest intermediate result was 9000000, which comfortably fits a 32-bit value.

#### 13.2.5. From Gregorian Date to CJDN (3)

Just like for the Julian calendar we can use a stepped combination of calendar levels. Compared to the Julian calendar we have to add the leap year rules for each 100 years and for each 400 years:

\begin{equation} \begin{split} c_2 \| = 30x_2 + \floorratio{7x_2 + 5}{12} − 2\floorratio{x_2 + 10}{12} + \floorratio{x_2 + 46}{48} \\ \| − \floorratio{x_2 + 1198}{1200} + \floorratio{x_2 + 4798}{4800} \end{split} \end{equation}

Here, too, we want to have all denominators be equal; then we get

\begin{equation} \begin{split} c_2 \| = 30x_2 + \floorratio{2800x_2 + 2000}{4800} − 2\floorratio{400x_2 + 4000}{12} \\ \| + \floorratio{100x_2 + 4600}{4800} − \floorratio{4x_2 + 4792}{4800} \\ \| + \floorratio{x_2 + 4798}{4800} \end{split} \end{equation}

which means calendar type 4 with $$q = 30$$, $$g = 4800$$, $$\{d_1,h_1,s_1\} = \{1,2800,2000\}$$, $$\{d_2,h_2,s_2\} = \{−2,400,4000\}$$, $$\{d_3,h_3,s_3\} = \{1,100,4600\}$$, $$\{d_4,h_4,s_4\} = \{−1,4,4792\}$$, $$\{d_5,h_5,s_5\} = \{1,1,4798\}$$.

The CJDN $$J_0$$ that corresponds to $$\{x_1,z_1,z_2\} = \{0,0,0\}$$, i.e., January 1st of year 0 (in the Gregorian calendar) is 1721060.

For the rest things are the same as for the Julian calendar. We then find

\begin{align} x_1 \| = j \\ z_1 \| = m − 1 \\ z_2 \| = d − 1 \\ c_1 \| = 12x_1 \\ y_1 \| = c_1 + z_1 \\ x_2 \| = y_1 \\ \| \begin{split} c_2 \| = 30x_2 + \floorratio{2800x_2 + 2000}{4800} − 2\floorratio{400x_2 + 4000}{4800} \\ \| + \floorratio{100x_2 + 4600}{4800} − \floorratio{4x_2 + 4792}{4800} + \floorratio{x_2 + 4798}{4800} \\ \| = 30x_2 + \floorratio{7x_2 + 5}{12} − 2\floorratio{x_2 + 10}{12} + \floorratio{x_2 + 46}{48} \\ \| − \floorratio{x_2 + 1198}{1200} + \floorratio{x_2 + 4798}{4800} \end{split} \\ y_2 \| = c_2 + z_2 \\ J \| = y_2 + J_0 = y_2 + 1721060 \end{align}

For example, which CJDN corresponds to Gregorian date July 6th, 2003? Then $$j = 2003$$, $$m = 7$$, $$d = 6$$, so $$x_1 = 2003$$, $$z_1 = 7 − 1 = 6$$, $$z_2 = 6 − 1 = 5$$, and then

\begin{align*} c_1 \| = 12×2003 = 24036 \\ y_1 \| = 24036 + 6 = 24042 \\ x_2 \| = 24042 \\ c_2 \| = 30×24042 + \floorratio{2800×24042 + 2000}{4800} − 2×\floorratio{400×24042 + 4000}{4800} \\ \| + \floorratio{100×24042 + 4600}{4800} − \floorratio{4×24042 + 4792}{4800} + \floorratio{24042 + 4798}{4800} \\ \| = 721260 + \floorratio{67319600}{4800} − 2×\floorratio{9620800}{4800} + \floorratio{2408800}{4800} − \floorratio{100960}{4800} + \floorratio{28840}{4800} \\ \| = 721260 + 14024 − 4008 + 501 − 21 + 6 = 731762 \\ y_2 \| = 731762 + 5 = 731767 \\ J \| = 731767 + 1721060 = 2452827 \end{align*}

We can combine the above formulas into

\begin{align} c \| = 12j + m \\ \| \begin{split} J \| = 30c + \floorratio{7c − 2}{12} − 2\floorratio{c + 9}{12} + \floorratio{c + 45}{48} \\ \| − \floorratio{c + 1197}{1200} + \floorratio{c + 4797}{4800} + d + 1721029 \end{split} \end{align}

#### 13.2.6. From CJDN to Gregorian Date (3)

We run through the same procedure as in the previous section, but in the opposite direction.

1. Calculate the running day number $$y_2$$ by subtracting the CJDN of running day number 0 from CJDN $$J$$.

\begin{equation} y_2 = J − J_0 = J − 1721060 \end{equation}

2. With $$q = 30$$, $$g = 4800$$, $$\{d_1,h_1,s_1\} = \{1,2800,2000\}$$, $$\{d_2,h_2,s_2\} = \{−2,400,4000\}$$, $$\{d_3,h_3,s_3\} = \{1,100,4600\}$$, $$\{d_4,h_4,s_4\} = \{−1,4,4792\}$$, $$\{d_5,h_5,s_5\} = \{1,1,4798\}$$ we have

$f = qg + ∑d_{i}h_i = 30×4800 + 1×2800 − 2×400 + 1×100 − 1×4 + 1×1 = 146097$

so the average month length is $$f/g = 146097/4800 = 30 + 699/1600$$ days. Also,

$∑d_{i}s_i = 1×2000 − 2×4000 + 1×4600 − 1×4792 + 1×4798 = −1394$

Translate to calculation year $$x_1$$, calculation month $$z_1$$, and calculation day $$z_2$$:

\begin{align} x_2 \| = c'_2(y_2) = \floorratio{4800y_2 + 15793}{146097} \label{eq:g3} \\ \| \begin{split} c_2 \| = 30x_2 + \floorratio{2800x_2 + 2000}{4800} − 2\floorratio{400x_2 + 4000}{4800} + \floorratio{100x_2 + 4600}{4800} \\ \| − \floorratio{4x_2 + 4792}{4800} + \floorratio{x_2 + 4798}{4800} \\ \| = 30x_2 + \floorratio{7x_2 + 5}{12} − 2\floorratio{x_2 + 10}{12} + \floorratio{x_2 + 46}{48} \\ \| − \floorratio{x_2 + 1198}{1200} + \floorratio{x_2 + 4798}{4800} \end{split} \\ z_2 \| = y_2 − c_2 \\ ζ \| = \floorratio{z_2}{33} \\ x_2 \| ↤ x_2 + ζ \\ \| \begin{split} c_2 \| ↤ 30x_2 + \floorratio{2800x_2 + 2000}{4800} − 2\floorratio{400x_2 + 4000}{4800} + \floorratio{100x_2 + 4600}{4800} \\ \| − \floorratio{4x_2 + 4792}{4800} + \floorratio{x_2 + 4798}{4800} \\ \| = 30x_2 + \floorratio{7x_2 + 5}{12} − 2\floorratio{x_2 + 10}{12} + \floorratio{x_2 + 46}{48} \\ \| − \floorratio{x_2 + 1198}{1200} + \floorratio{x_2 + 4798}{4800} \end{split} \\ z_2 \| ↤ y_2 − c_2 \\ y_1 \| = x_2 \\ x_1 \| = c'_1(y_1) = \floorratio{y_1}{12} \\ c_1 \| = 12x_1 \\ z_1 \| = y_1 − c_1 \end{align}

3. Then translate to year $$j$$, month $$m$$, and day $$d$$:

\begin{align} j \| = x_1 \\ m \| = z_1 + 1 \\ d \| = z_2 + 1 \end{align}

For example, which date in the Gregorian calendar corresponds to CJDN 2452827? Then $$J = 2452827$$ and then

\begin{align*} y_2 \| = J − 1721060 = 731767 \\ x_2 \| = \floorratio{4800×731767 + 15793}{46097} = \floorratio{3512497393}{146097} = 24042 \\ c_2 \| = 30×24042 + \floorratio{2800×24042 + 2000}{4800} − 2×\floorratio{400×24042 + 4000}{4800} \\ \| + \floorratio{100×24042 + 4600}{4800} − \floorratio{4×24042 + 4792}{4800} + \floorratio{24042 + 4798}{4800} \\ \| = 721260 + \floorratio{67319600}{4800} − 2×\floorratio{9620800}{4800} + \floorratio{2408800}{4800} − \floorratio{100960}{4800} + \floorratio{28840}{4800} \\ \| = 721260 + 14024 − 4008 + 501 − 21 + 6 = 731762 \\ z_2 \| = 731767 − 731762 = 5 \\ ζ \| = \floorratio{5}{33} = 0 \end{align*}

$$ζ = 0$$ so $$x_2$$, $$c_2$$, $$z_2$$ remain the same. Then

\begin{align*} y_1 \| = 24042 \\ x_1 \| = \floorratio{24042}{12} = 2003 \\ c_1 \| = 12×2003 = 24036 \\ z_1 \| = 24042 − 24036 = 6 \\ j \| = 2003 \\ m \| = 6 + 1 = 7 \\ d \| = 5 + 1 = 6 \end{align*}

The date is July 6th, 2003.

This can be condensed a bit to

\begin{align} y_2 \| = J − J_0 = J − 1721060 \\ x_2 \| = \floorratio{4800y_2 + 15793}{146097} \\ \| \begin{split} c_2 \| = 30x_2 + \floorratio{7x_2 + 5}{12} − 2\floorratio{x_2 + 10}{12} + \floorratio{x_2 + 46}{48} \\ \| − \floorratio{x_2 + 1198}{1200} + \floorratio{x_2 + 4798}{4800} \end{split} \\ z_2 \| = y_2 − c_2 \\ ζ \| = \floorratio{z_2}{33} \\ u \| = x_2 + ζ \\ \| \begin{split} d \| = y_2 − 30u − \floorratio{7u + 5}{12} + 2\floorratio{u + 10}{12} − \floorratio{u + 46}{48} \\ \| + \floorratio{u + 1198}{1200} − \floorratio{u + 4798}{4800} + 1 \end{split} \\ j \| = \floorratio{u}{12} \\ m \| = u − 12j + 1 \end{align}

In the calculation of $$x_2$$, the numerator can get very large. In a computer program that uses 32-bit values, the calculation of $$x_2$$ will yield wrong results when $$|y_2|$$ exceeds about 447,000 (days), which corresponds to only 1224 years. Section 12.2 explains how to find a solution for this. Such a solution is to replace the formula for $$x_2$$ with

\begin{align} q \| = \floorratio{y_2}{487} \\ r \| = y_2 \bmod 487 \\ x_2 \| = 16q + \floorratio{48q + 4800r + 15793}{146097} \end{align}

For example, if $$y_2 = 730,000$$, then $$x_2 = \floorratio{4800×730000 + 15793}{146097} = \floorratio{3504015793}{146097} = 23984$$, so then the greatest intermediate result is equal to 3,504,015,793, which is much greater than the greatest value (2,147,483,648) that fits in memory of 32 bits wide. With the alternative method, we find

\begin{align*} q \| = \floorratio{730000}{487} = 1498 \\ r \| = 730000 \bmod 487 = 474 \\ x_2 \| = 1498×16 + \floorratio{1498×48 + 4800×474 + 15793}{146097} = 23968 + \floorratio{2362897}{146097} = 23968 + 16 = 23984 \end{align*}

which is the same result as before, but now with the greatest intermediate result equal to 2,362,897, which is comfortably small enough to fit in 32 bits.

For the Gregorian calendar the formulas (for days and years) from the sections with "(2)" and "(3)" in the title aren't easier than the formulas from the section with "(1)" in the title, so we recommend the formulas from the sections with "(1)" in the title for general use for this calendar. In general, it is not always possible to find such formulas, so then it is nice to also have more complicated but also more powerful formulas available.

### 13.3. The Milanković Calendar

#### 13.3.1. From Milanković Date to CJDN

Some Eastern Orthodox Churches have for some time (since 1923) used a calendar invented by Milutin Milanković that only differs from the Gregorian Calendar in the rule about which century years are leap years. In the Gregorian Calendar those are all years whose number is not evenly divisible by 400. In the Milanković Calendar those are are years that when dividing by 900 leave a remainder equal to 200 or 600. The following table shows for the century years from 1500 through 2900 which of them are leap years in the Gregorian and Milanković Calendars.

 Year Gregorian Milanković 1500 no yes 1600 yes no 1700 no no 1800 no no 1900 no no 2000 yes yes 2100 no no 2200 no no 2300 no no 2400 yes yes 2500 no no 2600 no no 2700 no no 2800 yes no 2900 no yes

Between the years 1601 and 2799, the Milanković- and Gregorian calendars give the same results. The years 1600 and 2800 are leap years in the Gregorian calendar but not in the Milanković calendar.

With these rules, the Milanković calendar has a period of 365×900 + 900/4 − (900/100)×(7/9) = 328718 days. The conversion of a date to CJDN is nearly the same for the Milanković calendar as for the Gregorian calendar (see Section 13.2.1):

\begin{align} c_0 \| = \floorratio{m − 3}{12} \\ x_4 \| = j + c_0 \\ x_3 \| = \floorratio{x_4}{100} \\ x_2 \| = x_4 \bmod 100 \\ x_1 \| = m − 12c_0 − 3 \\ J \| = \floorratio{328718x_3 + 6}{9} + \floorratio{36525x_2}{100} + \floorratio{153x_1 + 2}{5} + d + 1721119 \end{align}

For example, which CJDN corresponds to Milanković date July 6th, 2003? Then $$j = 2003$$, $$m = 7$$, $$d = 6$$ hence

\begin{align*} c_0 \| = \floorratio{7 − 3}{12} = 0 \\ x_4 \| = 2003 + 0 = 2003 \\ x_3 \| = \floorratio{2003}{100} = 20 \\ x_2 \| = 2003 − 100×20 = 3 \\ x_1 \| = 7 − 12×0 − 3 = 4 \\ z_1 \| = 6 − 1 = 5 \\ c_1 \| = \floorratio{153×4 + 2}{5} = \floorratio{614}{5} = 122 \\ y_1 \| = 122 + 5 = 127 \\ z_2 \| = 127 \\ c_2 \| = \floorratio{36525×3}{100} = \floorratio{109575}{100} = 1095 \\ y_2 \| = 1095 + 127 = 1222 \\ z_3 \| = 1222 \\ c_3 \| = \floorratio{328718×20 + 6}{9} = \floorratio{6574366}{9} = 730485 \\ y_3 \| = 730485 + 1222 = 731707 \\ J \| = 731707 + 1721120 = 2452827 \end{align*}

#### 13.3.2. From CJDN to Milanković Date

Also in the opposite direction, we do the same as for the Gregorian calendar (see Section 13.2.2), except that we use different formulas to calculate $$x_3$$ and $$k_3$$:

\begin{align} \{x_3, r_3\} \| = \Div\left( 9×(J − 1721120) + 2, 328718 \right) \\ \{x_2, r_2\} \| = \Div(100r_3 + 99, 36525) \\ \{x_1, r_1\} \| = \Div(5r_2 + 2, 153) \\ c_0 \| = \floorratio{x_1 + 2}{12} \\ j \| = 100x_3 + x_2 + c_0 \\ m \| = x_1 − 12c_0 + 3 \\ d \| = \floorratio{k_1 \bmod 153}{5} + 1 \end{align}

For example, which date in the Gregorian calendar corresponds to CJDN 2452827? Then $$J = 2452827$$ and then

\begin{align*} \{x_3, r_3\} \| = \Div( 9×(2452827 − 1721120) + 2, 328718 ) \\ \| = \Div(6585365, 328718) = \{20, 11005\} \\ \{x_2, r_2\} \| = \Div\left( 100\floorratio{11005}{9} + 99, 36525 \right) \\ \| = \Div(100×1222 + 99, 36525) \\ \| = \Div(122299, 36525) = \{3, 12724\} \\ \{x_1, r_1\} \| = \Div\left( 5\floorratio{12724}{100} + 2, 153 \right) \\ \| = \Div(5×127 + 2, 153) \\ \| = \Div(637, 153) = \{4, 25\} \\ d \| = \floorratio{25}{5} + 1 = 6 \\ c_0 \| = \floorratio{4 + 2}{12} = 0 \\ j \| = 100×20 + 3 + 0 = 2003 \\ m \| = 4 − 12×0 + 3 = 7 \end{align*}

The date is July 6th, 2003.

### 13.4. The Egyptian Calendar

#### 13.4.1. From Egyptian Date to CJDN

The ancient Egyptians had a very simple calendar indeed, without leap years and with 30 days to every month, except that the last month had 5 days. In the calendar according to the Era of Nabonassar the first day (1 Thoth of year 1) was equivalent to 26 February −746 in the Julian calendar, i.e., to JD 1448638 ($$= J_0$$).

At the higher calendar level, each year has 365 days. At the lower level, each month has 30 days. The last month of the year actually has only 5 days, but we get that for free from the higher calendar level. We combine the two levels in the flat way, with $$z_2 = y_1$$.

For day $$d$$ of month $$m$$ of year $$j$$ we then find

\begin{align} x_2 \| = j − 1 \\ x_1 \| = m − 1 \\ z_1 \| = d − 1 \\ c_1 \| = 30x_1 \\ y_1 \| = c_1 + z_1 \\ z_2 \| = y_1 \\ c_2 \| = 365x_2 \\ y_2 \| = c_2 + z_2 \\ J \| = y_2 + J_0 \end{align}

This condenses to

\begin{equation} J = 365×(j − 1) + 30×(m − 1) + d − 1 + J_0 = 365j + 30m + d + 1448242 \end{equation}

For example, which CJDN corresponds to day 7 of month 5 of year 218 in the Egyptian calendar? Then we find

\begin{align*} x_2 \| = j − 1 = 218 − 1 = 217 \\ x_1 \| = m − 1 = 5 − 1 = 4 \\ z_1 \| = d − 1 = 7 − 1 = 6 \\ c_1 \| = 30x_1 = 30×4 = 120 \\ z_2 \| = y_1 = c_1 + z_1 = 120 + 6 = 126 \\ c_2 \| = 365x_2 = 365×217 = 79205 \\ y_2 \| = c_2 + z_2 = 79205 + 126 = 79331 \\ J \| = y_2 + J_0 = 79331 + 1448638 = 1527969 \end{align*}

so the answer is CJDN 1527969.

With the condensed formula we find

\begin{align*} J \| = 365j + 30m + d + 1448242 \\ \| = 365×218 + 30×5 + 7 + 1448242 \\ \| = 79570 + 150 + 7 + 1448242 = 1527969 \end{align*}

which is the same answer as before.

#### 13.4.2. From CJDN to Egyptian Date

In the opposite direction, the calculations are simple as well.

\begin{align} y_2 \| = J − J_0 \\ \{x_2, r_2\} \| = \Div(y_2, 365) \\ \{x_1, r_1\} \| = \Div(r_2, 30) \\ j \| = x_2 + 1 \\ m \| = x_1 + 1 \\ d \| = r_1 + 1 \end{align}

What date in the Egyptian calendar corresponds to CJDN 1527969? Then

\begin{align*} y_2 \| = J − J_0 = 1527969 − 1448638 = 79331 \\ \{x_2, r_2\} \| = \Div(79331, 365) = \{217, 126\} \\ \{x_1, r_1\} \| = \Div(126, 30) = \{4, 6\} \\ j \| = x_2 + 1 = 217 + 1 = 218 \\ m \| = x_1 + 1 = 4 + 1 = 5 \\ d \| = r_1 + 1 = 6 + 1 = 7 \end{align*}

so it is day 7 of month 5 of year 218.

### 13.5. The Babylonian Calendar

#### 13.5.1. From Babylonian Date to Julian Day Number

The Metonic Cycle says that 235 (synodical) months are equal to 19 (tropical) years of 12 or 13 months each, with 125 months of 30 days and 110 months of 29 days, so 6940 days in total. The long years (with 13 months) are the 1st, 4th, 7th, 9th, 12th, 15th, and 18th year of each cycle. In the 18th year, month 6 is doubled; in the other long years month 12 is doubled. Day 1 of month 1 (Nisannu) of year 1 of the era of Seleukos corresponds to 3 April −310 in the Julian Calendar, or CJDN 1607558.

The Babylonians determined the beginning of each month by looking for a particular phase of the Moon. They did not give the first month of each year the same number of days, and likewise for the other months, but did vary the number of months in each year according to the method described above. The distribution of months into years ran independently from the distribution of days into months.

If a calendar is (partly) based on direct observations, then it is a bit unpredictable and cannot be completely caught in formulas. The calendar then depends on things like the weather. If there happen to be many clouds in the sky one day, then the calendar officials may not see the moon at all that night, and then the official start of the new month may be delayed until after they see the moon the next night.

We derive a calendar that looks like the one of the Babylonians, but is entirely predictable. The difference with the historical calendar of the Babylonians should be at most 1 day.

That means a calendar with a stepped combination of a calendar level between day and month and a calendar level between month and year. We go from year number $$y$$ (the first year has number 1), month number $$m$$ (the first month has number 1), and day number $$d$$ (the first day has number 1) to running day number $$y_2$$ as follows:

\begin{align} x_1 \| = j − 1 \\ z_1 \| = m − 1 \\ z_2 \| = d − 1 \\ c_1 \| = \floorratio{235x_1 + 13}{19} \\ y_1 \| = c_1 + z_1 \\ x_2 \| = y_1 \\ c_2 \| = \floorratio{6940x_2}{235} \\ y_2 \| = c_2 + z_2 \\ J \| = y_2 + 1607558 \end{align}

This can be compressed to

\begin{align} y_1 \| = \floorratio{235j − 241}{19} + m \\ J \| = \floorratio{6940y_1}{235} + d + 1607557 \end{align}

The number of years since the beginning of the current Metonic cycle is equal to $$y_1 \bmod 19$$.

For example, which CJDN $$J$$ corresponds to year 3, month 9, day 27 of the era of Seleukos? Then $$j = 3$$, $$m = 9$$, $$d = 27$$, so

\begin{align*} y_1 \| = \floorratio{235×3 − 241}{19} + 9 = \floorratio{464}{19} + 9 = 24 + 9 = 33 \\ J \| = \floorratio{6940×33}{235} + 27 + 1607557 = \floorratio{229020}{235} + 1607584 = 974 + 1607584 = 1608558 \end{align*}

#### 13.5.2. From CJDN to Babylonian Date

Now we go in the opposite direction, from Julian Day number $$J$$ to day $$d$$, month $$m$$, year $$j$$ in the era of Seleukos.

\begin{align} y_2 \| = J − 1607558 \\ \{x_2, r_2\} \| = \Div(235y_2 + 234, 6940) \\ z_2 \| = \floorratio{r_2}{235} \\ y_1 \| = x_2 \\ \{x_1, r_1\} \| = \Div(19y_1 + 5, 235) \\ z_1 \| = \floorratio{r_1}{19} \\ j \| = x_1 + 1 \\ m \| = z_1 + 1 \\ d \| = z_2 + 1 \end{align}

For example, which date in the Babylonian calendar corresponds to CJDN $$1608558$$? Then $$J = 1608558$$, so

\begin{align*} y_2 \| = 1608558 − 1607558 = 1000 \\ \{x_2, r_2\} \| = \Div(235×1000 + 234, 6940) = \Div(235234, 6940) = \{33, 6214\} \\ z_2 \| = \floorratio{6214}{235} = 26 \\ \{x_1, r_1\} \| = \Div(19×33 + 5, 235) = \Div(632, 235) = \{2, 162\} \\ z_1 \| = \floorratio{162}{19} = 8 \\ j \| = 2 + 1 = 3 \\ m \| = 8 + 1 = 9 \\ d \| = 26 + 1 = 27 \end{align*}

which means day 27 of month 9 of year 3.

This can be compressed to

\begin{align} \{x_2, r_2\} \| = \Div(235×(J − 1607558) + 234, 6940) \\ \{x_1, r_1\} \| = \Div(19x_2 + 5, 235) \\ j \| = x_1 + 1 \\ m \| = \floorratio{r_1}{19} + 1 \\ d \| = \floorratio{r_2}{235} + 1 \end{align}

For example, which date in the Babylonian calendar corresponds to CJDN $$1608558$$? Then $$J = 1608558$$, so

\begin{align*} \{x_2, r_2\} \| = \Div(235×(1608558 − 1607558) + 234, 6940) = \Div(235234, 6940) = \{33, 6214\} \\ \{x_1, r_1\} \| = \Div(19×33 + 5, 235) = \Div(632, 235) = \{2, 162\} \\ j \| = 2 + 1 = 3 \\ m \| = \floorratio{162}{19} + 1 = 9 \\ d \| = \floorratio{6214}{235} + 1 = 27 \end{align*}

which means day 27 of month 9 of year 3.

### 13.6. The Jewish Calendar

#### 13.6.1. From Jewish Date to CJDN

The Jewish calendar is a lunisolar calendar, like the Babylonian calendar. The Jewish calendar is a lot more complicated than the Babylonian one, because

• the Jewish calendar has four rules that can delay the start of a new year, to ensure that certain calendar days do not fall on certain days of the week.
• the calendar year in the Jewish calendar can have six different lengths.
• the Jewish calendar has three different places where leap days can be inserted.
• the Jewish New Year does not fall at the beginning of the first month but at the beginning of the seventh month.

When it is necessary to indicate explicitly that a year is given according to the Jewish calendar, then A.M. (Anno Mundi, Year of the World) is used for that.

A calendar month has 29 or 30 days, a calendar year has 12 or 13 months, and a calendar day begins at 6 o'clock in the evening.

There are 6 different lengths of calendar years, namely 353, 354, 355, 383, 384, or 385 days. The years with 354 or 384 days are called regular. Years with one day more are called complete. Years with a day less are called deficient. Years with 355 or fewer days are called common, and years with more days are called embolismic.

New Year is the day at which the calendar year number increases by one. In the Jewish calendar, this is not the first day of month number 1, but the first day of month number 7, which is the month of Tishri.

##### 13.6.1.1. Calculation Year, Calculation Month, Calculation Day

For the calendrical calculations, it is convenient when each calendar period begins counting at 0. It is therefore inconvenient that the year number changes at the beginning of the 7th month. We calculate with a calculation year that begins with the 1st month (corresponding to calculation month number $$x_3 = 0$$). Months $$m$$ = 1 through 6 of the preceding calendar year $$j − 1$$ and months 7 through 12 or 13 of the current calendar year $$j$$ are part of calculation year $$x_1 = j − 1$$ as calculation months $$x_3$$ = 0 through 11 or 12. The first day of each month has calculation day number $$z_3 = 0$$. New Year's Day of calendar year $$j$$ corresponds to $$x_1 = j − 1$$, $$x_3 = 6$$, and $$z_4 = 0$$.

 $${m}$$ 1 2 3 4 5 6 7 8 9 10 11 12 13 $${x_3}$$ 0 1 2 3 4 5 6 7 8 9 10 11 12 $${x_1 − j}$$ 0 0 0 0 0 0 −1 −1 −1 −1 −1 −1 −1

For converting from year $$j$$, month $$m$$, day $$d$$ in the Jewish calendar to calculation year $$x_1$$, calculation month $$x_3$$, and calculation day $$z_4$$, we find

\begin{align} c_0 \| = \floorratio{13 − m}{7} \\ x_1 \| = j − 1 + c_0 \\ x_3 \| = m − 1 \\ z_4 \| = d − 1 \end{align}

For example, after the last day of month 13 of year 4682 in the Jewish calendar comes the first day of month 1 of year 4682. A few months later, after the last day of month 6 of year 4682 comes the first day of month 7 of year 4683. That day is New Year's Day. The following table shows a few correspondences.

$${j}$$ $${m}$$ $${d}$$ $${c_0}$$ $${x_1}$$ $${x_3}$$ $${z_4}$$
4682 13 29 0 4681 12 28
4682 1 1 1 4682 0 0
4682 6 29 1 4682 5 28
4683 7 1 0 4682 6 0
4683 12 30 0 4682 11 29

##### 13.6.1.2. Calendar Levels

Transforming a date from the Jewish calendar into CJDN requires combining four calendar levels:

1. First we calculate the running calculation month number $$c_1$$ of the first month (calculation month number $$z_1 = 0$$) of calculation year $$x_1$$.
2. Then we calculate the running day number $$c_2$$ of New Year (calculation day number 0, calculation month number 6) of calculation year $$x_1$$ from the running calculation month number $$c_1$$. With that, we can calculate the length of the year, and from that the length of all of the months.
3. Then we calculate $$y_3$$, the day number in the year, from month number $$x_3$$ in the calculation year.
4. Then we calculate the running day number $$y_4$$ of the desired date from the running day number $$c_2$$ of New Year and from the day number $$y_3$$ in the current calculation year and from the calculation day number $$z_4$$ in the calculation month.
5. And finally we add the CJDN of the day with $$y_4 = 0$$ to get the CJDN of the desired date.

##### 13.6.1.3. First CalendarLevel

At the first calendar level, $$y_1 = c_1(x_1) + z_1$$, where $$y_1$$ is the running month number, $$x_1$$ is the calculation year number, $$z_1$$ is the calculation month number in the calculation year, and $$c_1$$ is the running month number of the first month ($$z_1 = 0$$) of the calculation year.

The leap years (calendar years with 13 months) are the 3rd, 6th, 8th, 11th, 14th, 17th, and 19th year in each cycle of 19 years, which holds 235 months. The running month number $$c_1$$ of the first calculation month ($$z_1 = 0$$) of year $$x_1$$ is

\begin{equation} c_1 = \floorratio{235x_1 + 1}{19} \end{equation}

As an example we look at the beginning of the Jewish year A.M. 4682. Then $$x_1 = 4681$$ so

$c_1 = \floorratio{235×4681 + 1}{19} = \floorratio{1100036}{19} = 57896$

New Year of A.M. 4682 is 57896 months after New Year of A.M. 1.

If you're working in a calculation environment where whole numbers cannot be greater than a certain value $$w$$, then (to avoid overflow) $$|x_1|$$ in the preceding formula must not exceed $$w/235$$, instead of $$w$$. For $$w = 2^{31}$$, $$w/235$$ years correspond to 9,138,228 years. If that is too few for you, then you can find alternatives in the manner of Section 12.2. One alternative is, if $$w \gt 133$$

\begin{equation} c_1 = 12x_1 + \floorratio{7x_1 + 1}{19} \end{equation}

for which $$|x_1|$$ must not exceed $$w/7$$. Another alternative is, for $$w = 2^{31} = 2147483648$$

\begin{align} q \| = \floorratio{x_1}{3} \\ r \| = x_1 \bmod 3 \\ c_1 \| = 37q + \floorratio{2q + 235r + 1}{19} \end{align}

for which $$|x_1|$$ may have any value up to $$w$$.

##### 13.6.1.4. SecondCalendarLevel

At the second calendar level, $$y_2 = c_2(x_2) + z_2$$, where $$x_2 = c_1$$ is the running calculation month number of the first calculation month of the year, $$z_2$$ is the calculation day number in the current month of the first day of that month, and $$y_2$$ is the running day number of the first day of that month.

The epoch of the calendar (the beginning of 1 Tishri of year A.M. 1) is at 6 o'clock at night on Sunday 6 October −3760 in the Julian proleptic calendar, which corresponds to CJD 347997.75. For correspondences between calendar dates from different calendars we look at the situation at noon. Noon of the first day (New Year) of the Jewish calendar was on Monday 7 October −3760 in the Julian proleptic calendar (CJDN 347998 = $$J_0$$).

The beginning of the year is determined by the mean conjunction between the Sun and the Moon, where the length of a synodical month is set at 29 and 13753/25920 days, which is 765433/25920 days. The New Moon at the beginning of the month of Tishri (the New Year's month) of year A.M. 1 was at 5 hours, 11 minutes, and 20 seconds (= 5604/25920 days) after the beginning of the first day of that month (and that began at 6 o'clock at night).

The time $$μ$$ in days that elapsed since the beginning of the first day of the calendar and the New Moon at the beginning of the month with running month number $$c_1$$ is equal to

\begin{equation} μ = \frac{5604 + 765433c_1}{25920} \label{eq:μ} \end{equation}

The running day number $$υ_0$$ of the calendar day on which that New Moon occurs is

\begin{equation} υ_0 = ⌊μ⌋ \end{equation}

We again look at New Year of the Jewish year A.M. 4682. We earlier found that $$c_1 = 57896$$. Then

\begin{align*} μ \| = \frac{5604 + 765433×57896}{25920} = \frac{44315514572}{25920} = 1709703 \frac{12812}{25920} \\ υ_0 \| = 1709703 \end{align*}

If you're working in a calculation environment where whole numbers cannot exceed a certain value $$w$$, then (to avoid overflow) in equation \eqref{eq:μ} $$|c_1|$$ must not exceed $$w/765433$$, which is far less than $$w$$. For 32-bit numbers this corresponds to only about 226 jaar − far too few to be practical. Using the methods of Sec. 12.2 we can find better alternatives. One alternative, when $$w \gt 356,477,760$$, is

\begin{equation} μ = 29c_1 + \frac{5604 + 13753c_1}{25920} \end{equation}

for which $$|c_1|$$ must not exceeed $$w/13753$$, which for 32-bit numbers corresponds to about 156146 months, or about 12624 years. Another alternative, for $$w = 2^{31} = 2147483648$$, is

\begin{align} q \| = \floorratio{c_1}{1095} \\ r \| = c_1 \bmod 1095 \\ μ \| = 32336q + \frac{15q + 765433r + 5604}{25920} \end{align}

for which $$|c_1|$$ may have any value up to $$w$$, which corresponds to about 173 million years.

The following table shows some results.

$${x_1}$$ $${c_1}$$ $${q}$$ $${r}$$ $${μ}$$ $${υ_0}$$
4681 57896 52 956 $${1709703 \frac{12812}{25920}}$$ 1709703
4682 57909 52 969 $${1710087 \frac{10161}{25920}}$$ 1710087
4683 57921 52 981 $${1710441 \frac{19677}{25920}}$$ 1710441
5516 68224 62 334 $${2014695 \frac{12196}{25920}}$$ 2014695
5517 68236 62 346 $${2015049 \frac{21712}{25920}}$$ 2015049
5518 68249 62 359 $${2015433 \frac{19061}{25920}}$$ 2015433

The running day number $$υ_0$$ can be modified by four delays.

• The first delay occurs when the mean conjunction falls at or after noon and before the end of the calendar day at 6 o'clock at night. In that case, New Year is delayed by 1 day, the current year gets 1 day shorter, and the preceding year gets 1 day longer.

The time of day follows from $$μ \bmod 1$$, which is equal to 0 at the beginning of the calendar day at 6 o'clock at night, and is equal to 3/4 at noon. So if $$μ \bmod 1 ≥ 3/4$$, then New Year is delayed by one day. If $$μ \bmod 1 ≥ 3/4$$ then $$(μ \bmod 1) + 1/4 ≥ 1$$ so $$\left\lfloor μ + \frac{1}{4} \right\rfloor = ⌊μ⌋ + 1$$, so the running day number $$υ_1$$ of the first day of the month, including the effect of the first delay, is

\begin{equation} υ_1 = \left\lfloor μ + \frac{1}{4} \right\rfloor = \left\lfloor μ + \frac{6480}{25920} \right\rfloor = 29c_1 + \floorratio{12084 + 13753c_1}{25920} \label{eq:υ_1} \end{equation}

For A.M. 4682 we find

$υ_1 = \left\lfloor 1709703 + \frac{12812 + 6480}{25920} \right\rfloor = 1709703 + \floorratio{19292}{25920} = 1709703 = υ_0$

so that year's beginning is unaffected by the first delay.

If you're working in a calculation environment where whole numbers must not exceed some value $$w$$, then the same limitations hold here as in the previous example. You can then use the same alternatives as in the previous example, if you replace 5604 by 12084 there.

• The second delay occurs when the mean conjunction (after taking into account the first delay) falls on a Sunday, Wednesday, or Friday. In that case, New Year is delayed by 1 day, the current year gets one day shorter, and the preceding year gets one day longer.

$$υ_0 = υ_1 = 0$$ corresponds to CJDN 347998 which was a Monday, because $$347998 \bmod 7 = 0$$ and 0 corresponds to Monday. The second delay occurs when

\begin{equation} δ = υ_1 \bmod 7 \end{equation}

is equal to 2, 4, or 6.

We seek a formula for $$v_2(δ)$$ that returns 0 if $$δ$$ is equal to 0, 1, 3, or 5, and that returns 1 if $$δ$$ is equal to 2, 4, or 6. Because we only want 0 or 1 to be returned, something involving $$\bmod 2$$ seems to be needed. That we only want whole numbers as results points at something looking like $$⌊...δ...⌋ \bmod 2$$. The simplest formula that might work is then $$v_2(δ) = ⌊qδ⌋ \bmod 2$$ for a suitable factor $$q$$. Some searching shows that we get the desired outcomes when $$5/6 \lt q \lt 1$$. A few ratios with small denominators that are suitable for $$q$$ are 6/7, 7/8, 8/9, 9/10, 10/11, 11/12. The ratio with denominator 7 has the smallest denominator and has as additional bonus that it returns the desired values also if one calculates $$v_2(υ_1)$$ instead of $$v_2(δ) = v_2(υ_1 \bmod 7)$$: that saves one modulus calculation. With that, the running day number $$υ_2$$ of the first day of the month, including the first two delays, is equal to

\begin{equation} υ_2 = υ_1 + \left( \floorratio{6υ_1}{7} \bmod 2 \right) \label{eq:υ_2} \end{equation}

If you're working in a calculation environment where whole numbers must not exceed some value $$w$$, then $$υ_1$$ must not exceed $$w/6$$. For 32-bit numbers this corresponds to about 979,914 years. If that is not enough, then you can use $$v_2(υ_1 \bmod 7)$$ after all:

\begin{equation} υ_2 = υ_1 + \left( \floorratio{6×(υ_1 \bmod 7)}{7} \bmod 2 \right) \end{equation}

• The third delay is needed if a year, after treatment of the first two delays, would get a length of 356 days, which is deemed unacceptably long. By delaying New Year by 2 days, the length of the year is reduced to 354 days, which is acceptable. The preceding year (which was 353 or 383 days long) then gets 2 days longer (i.e., 355 or 385 days).

The length $$L_2(x_1)$$ of year $$x_1$$ (including the effects of the first two delays) is equal to

\begin{equation} L_2(x_1) = υ_2(x_1 + 1) − υ_2(x_1) \end{equation}

so New Year of year $$x_1$$ is delayed by 2 days if $$L_2(x_1) = 356$$. We achieve the desired effect with formula

\begin{equation} v_3 = 2\left( \floorratio{L_2 + 19}{15} \bmod 2 \right) \end{equation}

 $${L_2}$$ 353 354 355 356 382 383 384 385 $${v_3}$$ 0 0 0 2 0 0 0 0

• The fourth delay is needed when the preceding year (after treatment of the first two delays) would get a length of 382 days, which is deemed unacceptably short. By delaying New Year of the current year by 1 day the preceding year becomes 383 days long, which is acceptable. The current year (which was 355 days long) then gets one day shorter (i.e., 354 days).

So, New Year of year $$x_1$$ is delayed by 1 day when $$L_2(x_1 − 1) = 382$$. We get the desired effect with formula

\begin{equation} v_4 = \floorratio{L_2(x_1 − 1) + 7}{15} \bmod 2 \end{equation}

 $${L_2}$$ 353 354 355 356 382 383 384 385 $${v_4}$$ 0 0 0 0 1 0 0 0

Years do not exist for which $$L_2(x_1) = 356$$ and at the same time $$L_2(x_1 − 1) = 382$$, so the third and fourth delays are never active at the same time.

The running day number $$c_2$$ of New Year, taking into account all four delays, is

\begin{equation} c_2 = υ_2 + v_3 + v_4 \end{equation}

##### 13.6.1.5. The Third CalendarLevel

Some month lengths depend on the length of the year. With all delays taken into account, years can only have the following lengths $$L_4$$: 353, 354, 355, 383, 384, or 385 days.

For $$x_1 = 4681$$ we found earlier that $$υ_0 = 1709703$$. Equation \eqref{eq:υ_1} then yields $$υ_1 = 1709703 = υ_0$$, so the first delay does not affect that year. Equation \eqref{eq:υ_2} yields

\begin{align*} υ_2 \| = υ_1 + \left( \floorratio{6υ_1}{7} \bmod 2 \right) \\ \| = 1709703 + \left( \floorratio{6×1709703}{7} \bmod 2 \right) \\ \| = 1709703 + \left(\floorratio{10258218}{7} \bmod 2 \right) \\ \| = 1709703 + (1465459 \bmod 2) = 1709703 + 1 = 1709704 \end{align*}

so the second delay does have effect. The length of year $$x_1 = 4681$$ taking into account the first two delays is $$L_2(4681) = υ_2(4682) − υ_2(4681) = 1710087 − 1709704 = 383$$ days. The year with $$x_1 = 4682$$ therefore has a length (taking into account the first two delays) of 356 days, so the third delay applies, and New Year of that year is delayed by 2 days. The results for this and some other years are listed in the following table. The column with title "v" mentions the numbers of the delays that applied.

$${x_1}$$ $${υ_0}$$ $${υ_1}$$ $${υ_2}$$ $${L_2}$$ $${c_2}$$ $${L_4}$$ v
4681 1709703 1709703 1709704 383 1709704 385 2
4682 1710087 1710087 1710087 356 1710089 354 3
4683 1710441 1710442 1710443 353 1710443 353 1, 2
5516 2014695 2014695 2014696 355 2014696 355 2
5517 2015049 2015050 2015051 382 2015051 383 1, 2
5518 2015433 2015433 2015433 355 2015434 354 4

The first delay depends on the time of day, and the second delay depends on the day of the week, so those two delays combined depend on the time since the beginning of the week. The leap years depend on the position of the year in the current cycle of 19 years = 235 months. The calendar as a whole repeats itself when a whole number of weeks coincides with a whole number of leap cycles. The leap cycle has a length of 235×765433/25920 = 35975351/5184 days, so each period of 5184 cycles covers a whole number of days, namely 35975351. This number of days is not a multiple of 7, so the calendar only repeats itself after 7×35975351 = 251827457 days = 35975351 weeks = 689472 years = 8527680 months.

The distribution of the calendar year lengths in that period of 689472 years is listed in the following table, including how many of those years have that length if you take into account different delays.

353 354 355 356 382 383 384 385
$${υ_0}$$ 275583 159873 25984 228032
$${υ_1}$$ 275583 159873 25984 228032
$${υ_2}$$ 78738 140946 192933 22839 3712 116288 36288 97728
$${υ_4}$$ 69222 167497 198737 106677 36288 111051

The next table shows of how many years from that big cycle of 689472 years the New Year is affected by the delays mentioned in the column with title "v".

v
     # 
  %
−     
268937 39.0
1 98496 14.3
2 221616 32.1
3 22839 3.3
4 3712 0.5
1 + 2 73872 10.7
rest 0 0

The next table shows how often a year with a certain length follows a year with that same or another length. The columns are for the current year, and the rows are for the following year. For example, that a year of 354 days is followed by a year of 383 days occurs 40000 times in the great cycle of 689472 years.

 353 354 355 383 384 385 353 0 16404 16404 0 0 36414 354 13776 0 54468 53354 0 45899 355 9516 54491 16381 53323 36288 28738 383 0 40000 66677 0 0 0 384 29965 0 13323 0 0 0 385 22965 56602 31484 0 0 0

We now know the running day number $$c_2$$ of New Year (1 Tishri) of calculation year $$x_1$$.

The length $$L$$ of calendar year $$x_1$$ is then equal to

\begin{equation} L = c_2(x_1 + 1) − c_2(x_1) \end{equation}

The month lengths are as follow, in years with different lengths:

$${m}$$ 353 354 355 383 384 385
1 Nisan 30 30 30 30 30 30
2 Iyar 29 29 29 29 29 29
3 Sivan 30 30 30 30 30 30
4 Tammuz 29 29 29 29 29 29
5 Av 30 30 30 30 30 30
6 Elul 29 29 29 29 29 29
7 Tishri 30 30 30 30 30 30
8 Ḥeshvan 29 29 30 29 29 30
9 Kislev 29 30 30 29 30 30
10 Tevet 29 29 29 29 29 29
11 Shevat 30 30 30 30 30 30
12 Adar Ⅰ 0 0 0 30 30 30
13 Adar Ⅱ 29 29 29 29 29 29

In a year of 13 months, the embolismic (extra) month Adar Ⅰ is inserted between Shevat and the original Adar, which is then renamed to Adar Ⅱ.

The lengths of the months do not fit a simple calendar (of types 1 or 2 from Sec. 12.11), because the number of successive months with the same length varies by more than one. In the first seven months, the number of successive months with the same length is equal to 1, but after that it is sometimes equal to 3. For example, months 8 through 10 in a year of 353 or 383 days are all 29 days long, and months 7 through 9 in a year of 355 or 385 days are all 30 days long.

The lengths that the months have in a year of 384 days long fit the equation

\begin{equation} y_3 = \floorratio{384x_3 + 7}{13} + z_3 \end{equation}

where $$x_3$$ is the calculation month number in the year, $$y_3$$ is the running day number in the year, $$z_3$$ is the running day number in the month, and all of them begin at 0 for the first month and day.

This formula can also be used for month lengths in a year of 354 days, when the length of the year terminates the 12th month at 29 days.

A correction to this formula is needed for years with a length $$L$$ unequal to 354 or 384 days. In a year with 353 or 383 days, month 9 must be one day shorter. In a year with 355 or 385 days, month 8 must be one day longer. The corrections $$c_8$$ for month 8 and $$c_9$$ for month 9 as a function of year length $$L$$ are

 $${L}$$ 353 354 355 383 384 385 $${c_8}$$ 0 0 1 0 0 1 $${c_9}$$ −1 0 0 −1 0 0

We find the following formula for the month−8 correction

\begin{equation} c_8 = \floorratio{L + 353}{2} \bmod 15 = \floorratio{L + 7}{2} \bmod 15 \end{equation}

and for the month−9 correction

\begin{equation} c_9 = −\left( \floorratio{385 − L}{2} \bmod 15 \right) \end{equation}

We calculate the running day number of a date in months 1 through 6 ($$x_3$$ = 0 through 5) from the following New Year (the first day of month 7), and the length of those months is the same in every year, so the month-8 and month-9 corrections do not affect such dates. We calculate the running day number of a date in months 7 through 13 ($$x_3$$ = 6 through 12) from the preceding New Year. The month-8 correction applies to dates in months 9 through 13, and the month-9 correction to dates in months 10 through 13. In the following table, $$f_8$$ shows (with value 1) to which months the month-8 correction applies, and $$f_9$$ likewise for the month-9 correction.

 $${x_3}$$ 0 1 2 3 4 5 6 7 8 9 10 11 12 $${f_8}$$ 0 0 0 0 0 0 0 0 1 1 1 1 1 $${f_9}$$ 0 0 0 0 0 0 0 0 0 1 1 1 1

We get that using the equations

\begin{align} f_8 \| = \floorratio{x_3 + 4}{12} \\ f_9 \| = \floorratio{x_3 + 3}{12} \end{align}

With that, the formula to go from month number $$x_3$$ in the calculation year to day number $$c_3$$ in the calculation year of the first day of that month is

\begin{equation} c_3 = \floorratio{384x_3 + 7}{13} + c_8\floorratio{x_3 + 4}{12} + c_9\floorratio{x_3 + 3}{12} \end{equation}

and we add the day number $$z_3$$ in the month to get that day's day number $$y_3$$ in the calculation year

\begin{equation} y_3 = c_3 + z_3 \end{equation}

##### 13.6.1.6. The Fourth CalendarLevel

The running day number $$c_4$$ (since 1 Tishri of A.M. 1) of the first day of the desired month is

\begin{equation} c_4 = c_2 + c_3 \label{eq:c_4} \end{equation}

The first six months (from Nisan through Elul) together always have 177 days, regardless of the length of the calendar year, so the running day number $$c_2$$ of 1 Tishri of the calculation year is 177 days greater than the running day number of 1 Nisan of that same calculation year. So, the running day number $$y_4$$ (since 1 Nisan of A.M. 1) of the desired date is

\begin{equation} y_4 = c_4 + z_3 − 177 \end{equation}

The epoch of the calendar is 1 Tishri of the year A.M. 1 and corresponds to CJDN 347998 = $$J_0$$. So, the CJDN $$J$$ of the desired date is

\begin{equation} J = J_0 + y_4 = J_0 + c_2 + c_3 + z_3 − 177 = 347821 + c_2 + c_3 + z_3 \end{equation}

#### 13.6.2. From CJDN to Jewish Calendar

Now we go in the opposite direction.

1. Transform CJDN $$J$$ into running day number $$y_4$$ since 1 Tishri A.M. 1:

\begin{equation} y_4 = J − J_0 + 177 = J − 347821 \end{equation}

As examples we'll calculate the Hebrew calendar dates that correspond to the following CJDN: 2000087, 2001327, 2057986. For $$J = 2057986$$ we find $$y_4 = 2057986 − 347821 = 1710165$$. All in all we find

$${J}$$ $${y_4}$$
2000087 1652266
2001327 1653506
2057986 1710165
119311997 118964176

2. We need to take into account the effect of all delays to determine in which month the day with a certain running day number belongs, but the effect of the third and fourth delays can only be calculated for a specific month in a specific year, and that month and year are the very items we're trying to figure out. So, we need to first estimate a month and year without taking into account the third and fourth delays, and then calculate what those delays are, and then see if our estimate was good or bad.

Using the inverse of equation \eqref{eq:υ_1} we can calculate the provisional running calculation month number $$y_1'$$ for running day number $$y_4$$ as if the second, third, and fourth delays do not exist:

\begin{equation} y_1' = \floorratio{25920y_4 + 25920 − 12084 − 1}{765433} = \floorratio{25920y_4 + 13835}{765433} \label{eq:y_1p} \end{equation}

For $$y_4 = 1710165$$ we find $$y_1' = \floorratio{25920×1710165 + 13835}{765433} = \floorratio{44327490635}{765433} = 57911$$.

If you're working in a calculation environment in which whole numbers must not exceed a certain value $$w$$, then (to avoid overflow) in equation \eqref{eq:y_1p} $$|y_4|$$ must not exceed $$w/25920$$, which is much less than $$w$$. For 32-bit numbers this corresponds to about 226 years − far too few to be practical. Using Sec. 12.2 we can find better alternatives. One alternative, when $$w = 2^{31} = 2147483648$$, is

\begin{align} q \| = \floorratio{y_4}{1447} \\ r \| = y_4 \bmod 1447 \\ y_1' \| = 49q + \floorratio{23q + 25920r + 13835}{765433} \end{align}

for which $$|y_4|$$ may have any value up to $$w$$, which corresponds to about 5.9 million years.

For $$y_4 = 1710165$$ we find

\begin{align*} q \| = \floorratio{1710165}{1447} = 1181 \\ r \| = 1710165 \bmod 1447 = 1258 \\ y_1' \| = 49×1181 + \floorratio{23×1181 + 25920×1258 + 13835}{765433} \\ \| = 57869 + \floorratio{32648358}{765433} = 57869 + 42 = 57911 \end{align*}

just like before.

For all example CJDN we find

$${J}$$ $${q}$$ $${r}$$ $${y_1'}$$
2000087 1141 1239 55951
2001327 1142 1032 55992
2057986 1181 1258 57911
119311997 82214 518 4028506

If we calculate the running day number $$c_4(y_1')$$ of the first day of that running calculation month with all applicable delays included, and then calculate the provisional day-number-in-the-month $$y_4 − c_4(y_1')$$, then we find results that vary between −2 en 29. The days for which this day number is negative belong in the previous month (with $$y_1 = y_1' − 1$$). There are 5963491 such days in each great calendar cycle of 251827457 days, which means that for one in about every 42 days $$y_1'$$ is one too great.

The days for which this day number is not less than 0 and not greater than 28 certainly belong in the current month (with $$y_1 = y_1'$$), because all months have at least 29 days. The days for which this day number is equal to 29 (which indicates the 30th day of the month, because the first day of the month has calculation day number 0) can belong in the current month, but can also belong in the next month, because some months have only 29 days.

If we calculate the day number in the month compared to the next month (i.e., $$y_4 − c_4(y_1' + 1)$$), then we find results that vary between −32 and 0. The days for which this day number is equal to 0 belong in this next month (with $$y_1 = y_1' + 1$$); There really are days for which the provisional running month number $$y_1'$$ is too small. There are 163258 such days in each great calendar cycle of 251827457 days, so for roughly one out of 1543 days (about one day out of every 52 months) $$y_1'$$ is one too small. For 97.6% of days, $$y_1'$$ is already the correct month.

Unfortunately, there is no simple relationship between the day number relative to month $$y_1'$$ and that relative to month $$y_1' + 1$$, so we must calculate both day numbers to figure out the month into which the desired day falls.

The minimal calculation is as follows: Calculate $$y_4 − c_4(y_1')$$. If that value is negative, then $$y_1 = y_1' − 1$$. If that value is not negative and is not greater than 28, then $$y_1 = y_1'$$. If that value is greater than 28, then also calculate $$y_4 − c_4(y_1' + 1)$$. If that second value is negative, then $$y_1 = y_1'$$. If that second value is not negative, then $$y_1 = y_1' + 1$$.

The number of calculations is then not the same for all dates. If it is advantageous to have the same number of calculations for all dates, then calculate as described below.

3. Calculate the provisional calculation year number $$ξ_1$$ and calculation month number $$μ_1$$ in the calculation year, for running calculation month number $$γ_1$$:

\begin{align} γ_1 \| = y_1' + 1 \\ κ_1 \| = 19γ_1 + 17 \\ ξ_1 \| = \floorratio{κ_1}{235} \\ μ_1 \| = \floorratio{κ_1 \bmod 235}{19} \end{align}

For our first example CJDN we had $$y_1' = 57911$$. Then

\begin{align*} γ_1 \| = y_1' + 1 = 57912 \\ κ_1 \| = 19×57912 + 17 = 1100345 \\ ξ_1 \| = \floorratio{1100345}{235} = 4682 \\ μ_1 \| = \floorratio{1100345 \bmod 235}{19} = \floorratio{75}{19} = 3 \end{align*}

For all example CJDNs we find

$${J}$$ $${y_1'}$$ $${γ_1}$$ $${κ_1}$$ $${ξ_1}$$ $${μ_1}$$
2000087 55951 55952 1063105 4523 10
2001327 55992 55993 1063884 4527 2
2057986 57911 57912 1100345 4682 3
119311997 4028506 4028507 76541650 325709 1

4. Calculate the running day number $$c_4'(ξ_1,μ_1)$$ of the beginning of calculation month $$μ_1$$ of calculation year $$ξ_1$$ in the way described in the previous section, and then the day number $$ζ_1$$ of the month, relative to $$c_4'(ξ_1,μ_1)$$:

\begin{equation} ζ_1 = y_4 − c_4'(ξ_1,μ_1) \end{equation}

Apply a correction in case $$ζ_1$$ turns out to be negative:

\begin{equation} γ_2 = γ_1 + \floorratio{ζ_1}{33} \end{equation}

If $$ζ_1$$ is not negative, then the correction is equal to 0.

For calculation year 4682 we found earlier that it has a length of $$L = 354$$ days, and that its New Year has for its running day number $$c_2' = 1710089$$. For calculation month $$μ_1 = 3$$ the month-8 and month-9 corrections $$c_8$$, $$c_9$$ are equal to 0 and we find

\begin{align*} c_3' \| = \floorratio{384μ_1 + 7}{13} + c_8×\floorratio{μ_1 + 4}{12} + c_9×\floorratio{μ_1 + 3}{12} \\ \| = \floorratio{384×3 + 7}{13} = \floorratio{1159}{13} = 89 \\ c_4' \| = c_2' + c_3' = 1710089 + 89 = 1710178 \\ ζ_1 \| = y_4 − c_4' = 1710165 − 1710178 = −13 \end{align*}

so the target day does not belong to calculation month 3 of calculation year 4682. Then

$γ_2 = γ_1 + \floorratio{ζ_1}{33} = 57912 + \floorratio{−13}{33} = 57912 + (−1) = 57911$

For all example CJDNs we find

$${J}$$ $${y_4}$$ $${γ_1}$$ $${ξ_1}$$ $${μ_1}$$ $${c_4'}$$ $${ζ_1}$$ $${⌊ζ_1/33⌋}$$ $${γ_2}$$
2000087 1652266 55952 4523 10 1652296 −30 −1 55951
2001327 1653506 55993 4527 2 1653506 0 0 55993
2057986 1710165 57912 4682 3 1710178 −13 −1 57911
119311997 118964176 4028507 325709 1 118964207 −31 −1 4028506

so $$y_1'$$ was one too small for $$J = 2001327$$.

5. Calculate in the same fashion the calculation year number $$ξ_2$$, calculation month number $$μ_2$$, running day number $$c_4'(ξ_2,μ_2)$$ of the first day of that month, day-number-in-the-month $$ζ_2$$, running month number $$γ_3$$, calculation year number $$ξ_3$$, calculation month number $$μ_3$$, running day number $$c_4'(ξ_3,μ_3)$$ of the first day of that month, and day-number-in-the-month $$ζ_3$$. The final calculation year number is then

\begin{equation} x_1 = ξ_3 \end{equation}

and the final calculation month number is

\begin{equation} x_3 = μ_3 \end{equation}

and the final calculation day number is

\begin{equation} z_4 = ζ_3 \end{equation}

Running month number $$γ_2 = 57911$$ corresponds to

\begin{align*} ξ_2 \| = \floorratio{19×57911 + 17}{235} = \floorratio{1100326}{235} = 4682 \\ μ_2 \| = γ_2 − \floorratio{235ξ_2 + 1}{19} \\ \| = 57911 − \floorratio{235×4682 + 1}{19} \\ \| = 57911 − \floorratio{1100271}{19} = 57911 − 57909 = 2 \end{align*}

For calculation year 4682 we earlier found that it has a length of $$L = 354$$ days, and that its New Year has for its running day number $$c_2' = 1710089$$. For calculation month $$μ_2 = 2$$ the month-8 and month-9 corrections $$c_8$$, $$c_9$$ are equal to 0, and we find

\begin{align*} c_3' \| = \floorratio{384μ_2 + 7}{13} + c_8\floorratio{μ_2 + 4}{12} + c_9\floorratio{μ_2 + 3}{12} \\ \| = \floorratio{384×2 + 7}{13} \\ \| = \floorratio{775}{13} = 59 \\ c_4' \| = c_2' + c_3' = 1710089 + 59 = 1710148 \\ ζ_2 \| = y_4 − c_4' = 1710165 − 1710148 = 17 \end{align*}

This is not negative and also not greater than 28, so we have found the correct month.

For all example CJDNs we find

$${J}$$ $${y_4}$$ $${γ_1}$$ $${γ_2}$$ $${ξ_2}$$ $${μ_2}$$ $${c_4'}$$ $${ζ_2}$$ $${γ_3}$$ $${x_1=ξ_3}$$ $${x_3=μ_3}$$ $${c_4'}$$ $${z_4=ζ_3}$$
2000087 1652266 55952 55951 4523 9 1652267 −1 55950 4523 8 1652237 29
2001327 1653506 55993 55993 4527 2 1653506 0 55993 4527 2 1653506 0
2057986 1710165 57912 57911 4682 2 1710148 17 57911 4682 2 1710148 17
119311997 118964176 4028507 4028506 325709 0 118964177 −1 4028505 325708 12 118964148 28

so $$y_1'$$ was correct for $$J = 2057986$$, and $$y_1'$$ was one too great for $$J = 2000087$$.

6. Transform calculation year $$x_1$$, calculation month $$x_3$$, and calculation day $$z_4$$ into calendar year $$j$$, calendar month $$m$$, and calendar day $$d$$:

\begin{align} c \| = \floorratio{12 − x_3}{7} \\ j \| = x_1 + 1 − c \\ m \| = x_3 + 1 \\ d \| = z_4 + 1 \end{align}

For calculation year 4682, calculation month 2, calculation day 17 we find

\begin{align*} c \| = \floorratio{12 − 2}{7} = \floorratio{10}{7} = 1 \\ j \| = x_1 + 1 − c = 4682 + 1 − 1 = 4682 \\ m \| = 2 + 1 = 3 \\ d \| = 17 + 1 = 18 \end{align*}

so the date is day 18 of calendar month 3 (Sivan) of year 4682.

For all example-CJDNs we find

$${J}$$ $${x_1}$$ $${x_3}$$ $${z_4}$$ $${c}$$ $${j}$$ $${m}$$ $${d}$$
2000087 4523 8 29 0 4524 9 30
2001327 4527 2 0 1 4527 3 1
2057986 4682 2 17 1 4682 3 18
119311997 325708 12 28 0 325709 13 29

### 13.7. A LunarCalendar With Many Fixed Month Lengths

If the distribution of days across months is independent from the distribution of months across years, then a particular month of the year won't have the same length in all years. For example, the first month of year 1 of the Babylonian calendar has 29 days, but the first month of year 2 has 30 days. For doing calendar calculations in your head, it would be convenient if most months have the same length in each year. Can we construct a lunisolar calendar that has that?

With the characteristics of the Cycle of Meton it is not possible to make each month have the same length in every year that contains it. If we make a fixed number of the first 12 months of every year long (with 30 days), then the corresponding number of long months in the 19-year cycle is a multiple of 19. If the 13th month were a short one, then the total number of long months would be a multiple of 19, and if the 13th month were a long one, then the total number would be 7 (the number of years in the 19-year cycle that have 13 months) plus a multiple of 19, but the Cycle of Meton requires 125 = 6×19 + 11 long months, which fits neither of the two possibilities. We'll have to vary the length of some of the first 12 months of every year, too, if we want most calendar months in each year to have a fixed length. We want to use only months of 29 or 30 days.

If we want to use the calendar calculation techniques discussed before, then it is most convenient if we need only a single calendar level for the calculations between months and days, but then we need both short and long years to use the same straight line and thus to have the same month lengths, apart from the shortening at the very end of the year.

We saw before that 6 long months in the first 12 months of the year is not enough, so we must use a formula that yields 7 long months in the first 12 months. With 7 long months in the first 12 months of every year, we have 7×12 = 133 long months in the 19-year cycle, and that is too many, because the Cycle of Meton only has 125 long months in 19 years. So, the 13th month must be a short month, and also we have to change 8 long months (of 30 days) into short months (of 29 days) in the short years (of 12 months). We can do that by making short years one day shorter, but then month 12 must be a long month, otherwise month 12 would have only 28 days in short years.

We can arrange all of that with the following three types of years in the 19-year cycle:

• 7 years of 13 months with 384 days (7 long months, 6 short months).

• 4 years of 12 months with 355 days (7 long months, 5 short months).

• 8 years of 12 months with 354 days (6 long months, 6 short months).

Then there are in total 7×13 + 4×12 + 8×12 = 235 months, which is as expected. There are then 7×7 + 4×7 + 8×6 = 125 long months, which is correct.

The straight line must yield a long month 12 (with 30 days) and a short month 13 (with 29 days), and that 7 of the first 12 months are long months.

The following formula provides this (with $$x_1$$ the number of months since the first month, and $$c_1$$ the number of days from the first day of the year to the first day of month $$x_1$$):

\begin{equation} c_1 = 29x_1 + \floorratio{7x_1 + 7}{13} = \floorratio{384x_1 + 7}{13} \label{eq:metonmaand} \end{equation}

For example, if $$x_1 = 4$$, then equation \eqref{eq:metonmaand} yields

$c_1 = \floorratio{384×4 + 7}{13} = \floorratio{1543}{13} = 118$

so the first day of the fifth month ($$x_1 = 4$$) is the 118th day since the first day of the year.

For the first day of the first 14 months, equation \eqref{eq:metonmaand} gives the following results (with $$L$$ the length of the month):

 $${m}$$ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $${x_1}$$ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 $${c_1}$$ 0 30 59 89 118 148 177 207 236 266 295 325 355 384 $${L}$$ 30 29 30 29 30 29 30 29 30 29 30 30 29

Now we need a second calendar level to get the right number of days in each year. We can use the following formula for that, where $$x_2$$ is the number of years since the first year, and $$c_2$$ is the running day number of the first day of the year:

\begin{equation} c_2 = 354x_2 + 30\floorratio{7x_2 + 1}{19} + \floorratio{4x_2 + 12}{19} \label{eq:metonjaar} \end{equation}

For example, if $$x_2 = 8$$, then equation \eqref{eq:metonjaar} yields

$c_2 = 354×8 + 30\floorratio{7×8 + 1}{19} + \floorratio{4×8 + 12}{19} = 2832 + 30×3 + 2 = 2837$

so the first day of year $$x_2 = 9$$ has running day number 2837.

This is calendar type 4, with $$q = 354$$, $$g = 19$$, $$\{d_1,h_1,s_1\} = \{30,7,1\}$$, $$\{d_2,h_2,s_2\} = \{1,4,12\}$$.

The complete algorithm is then, from year $$j$$, month $$m$$, day $$d$$ to running day number $$y_2$$:

\begin{align} x_2 \| = j − 1 \\ x_1 \| = m − 1 \\ z_1 \| = d − 1 \\ c_1 \| = \floorratio{384x_1 + 7}{13} \\ y_1 \| = c_1 + z_1 \\ z_2 \| = y_1 \\ c_2 \| = 354x_2 + 30\floorratio{7x_2 + 1}{19} + \floorratio{4x_2 + 12}{19} \\ y_2 \| = c_2 + z_2 \end{align}

For example, which running day number corresponds to day 27 of month 9 of year 3 in this calendar? Then

\begin{align*} j \| = 3 \\ m \| = 9 \\ d \| = 27 \\ x_2 \| = 3 − 1 = 2 \\ x_1 \| = 9 − 1 = 8 \\ z_1 \| = 27 − 1 = 26 \\ c_1 \| = \floorratio{384×8 + 7}{13} = \floorratio{3079}{13} = 236 \\ y_1 \| = 236 + 26 = 262 \\ z_2 \| = 262 \\ c_2 \| = 354×2 + 30×\floorratio{7×2 + 1}{19} + \floorratio{4×2 + 12}{19} \\ \| = 708 + 30×\floorratio{15}{19} + \floorratio{20}{19} = 708 + 0 + 1 = 709 \\ y_2 \| = 709 + 262 = 971 \end{align*}

This can be compressed to

\begin{equation} y_2 = 354j + 29m + 30\floorratio{7j − 6}{19} + \floorratio{4j + 8}{19} + \floorratio{7m}{13} + d − 384 \end{equation}

For the same date as before:

\begin{align*} y_2 \| = 354×3 + 29×9 + 30\floorratio{7×3 − 6}{19} + \floorratio{4×3 + 8}{19} + \floorratio{7×9}{13} + 27 − 384 \\ \| = 1323 + 30\floorratio{15}{19} + \floorratio{20}{19} + \floorratio{63}{13} − 357 \\ \| = 1323 + 0 + 1 + 4 − 357 = 971 \end{align*}

In the opposite direction, the formulas are

\begin{align} x_2 \| = \floorratio{19y_2 + 546}{6940} \\ c_2 \| = 354x_2 + 30\floorratio{7x_2 + 1}{19} + \floorratio{4x_2 + 12}{19} \\ z_2 \| = y_2 − c_2 \\ ζ \| = \floorratio{z_2}{385} \\ x_2 \| ↤ x_2 + ζ \\ c_2 \| ↤ 354x_2 + 30\floorratio{7x_2 + 1}{19} + \floorratio{4x_2 + 12}{19} \\ z_2 \| ↤ y_2 − c_2 \\ y_1 \| = z_2 \\ k_1 \| = 13y_1 + 5 \\ x_1 \| = \floorratio{k_1}{384} \\ z_1 \| = \floorratio{k_1 \bmod 384}{13} \\ j \| = x_2 + 1 \\ m \| = x_1 + 1 \\ d \| = z_1 + 1 \end{align}

For example, which day in this calendar corresponds to running day number 971? Then $$y_2 = 971$$ and then

\begin{align*} x_2 \| = \floorratio{19×971 + 546}{6940} = \floorratio{18995}{6940} = 2 \\ c_2 \| = 354×2 + 30×\floorratio{7×2 + 1}{19} + \floorratio{4×2 + 12}{19} \\ \| = 708 + 30×\floorratio{15}{19} + \floorratio{20}{19} = 708 + 0 + 1 = 709 \\ z_2 \| = 971 − 709 = 262 \\ ζ \| = \floorratio{262}{385} = 0 \end{align*}

$$ζ = 0$$ so $$x_2$$, $$c_2$$, $$z_2$$ remain the same. Then

\begin{align*} y_1 \| = 262 \\ k_1 \| = 13×262 + 5 = 3411 \\ x_1 \| = \floorratio{3411}{384} = 8 \\ z_1 \| = \floorratio{3411 \bmod 384}{13} = \floorratio{339}{13} = 26 \\ j \| = 2 + 1 = 3 \\ m \| = 8 + 1 = 9 \\ d \| = 26 + 1 = 27 \end{align*}

so the date is day 27 of month 9 of year 3.

This can be compressed a little, to

\begin{align} x' \| = \floorratio{19y_2 + 546}{6940} \\ x_2 \| = x' + \floorratio{y_2 − 354x' − 30\floorratio{7x' + 1}{19} − \floorratio{4x' + 12}{19}}{385} \\ k_1 \| = 13×\left( y_2 − 354x_2 − 30\floorratio{7x_2 + 1}{19} − \floorratio{4x_2 + 12}{19} \right) + 5 \\ j \| = x_2 + 1 \\ m \| = \floorratio{k_1}{384} + 1 \\ d \| = \floorratio{k_1 \bmod 384}{13} + 1 \end{align}

For example, which day in this calendar corresponds to running day number 971? Then $$y_2 = 971$$ and then

\begin{align*} x' \| = \floorratio{19×971 + 546}{6940} = \floorratio{18995}{6940} = 2 \\ x_2 \| = 2 + \floorratio{971 − 354×2 − 30 \floorratio{7×2 + 1}{19} − \floorratio{4×2 + 12}{19}}{385} \\ \| = 2 + \floorratio{263 − 30 \floorratio{15}{19} − \floorratio{20}{19}}{385} \\ \| = 2 + \floorratio{263 − 0 − 1}{385} = 2 \\ k_1 \| = 13×\left( 971 − 354×2 − 30\floorratio{7×2 + 1}{19} − \floorratio{4×2 + 12}{19} \right) + 5 \\ \| = 13×\left( 263 − 30\floorratio{15}{19} − \floorratio{20}{19} \right) + 5 \\ \| = 13×(263 − 0 − 1) + 5 = 3411 \\ j \| = 2 + 1 = 3 \\ m \| = \floorratio{3411}{384} + 1 = 9 \\ d \| = \floorratio{3411 \bmod 384}{13} + 1 = \floorratio{339}{13} + 1 = 27 \end{align*}

so the date is day 27 of month 9 of year 3.

### 13.8. The Islamic Calendar

#### 13.8.1. From Islamic Date to CJDN

The religious Islamic calendar depends on observations and so cannot be caught in formulas. The administrative calendar has fixed rules so it can be caught in formulas. In the administrative calendar, the odd months have 30 days and the even months have 29 days, except that the last month sometimes has 29 days and sometimes has 30 days. This gives a year 354 or 355 days. Of each 30 years, 11 are long (with 355 days) and the remainder are short (with 354 days). A complete cycle contains 11×355 + 19×354 = 10631 days.

Rob van Gent (at //www.staff.science.uu.nl/~gent0113/islam/islam_tabcal.htm) mentions four different ways that are used to distribute the 11 leap years across each cycle of 30 years. Table 1 lists those ways and their leap year distributions, and shows the corresponding $$r$$ (as in equation \eqref{eq:r}).

Table 1: Leap Years in Islamic Calendar

Type Leap Years $${r}$$ Origin/Use
I 2 5 7 10 13 15 18 21 24 26 29 15 Kūshyār ibn Labbān, Ulugh Beg, "Kuwaiti Algorithm"
II 2 5 7 10 13 16 18 21 24 26 29 14 al-Fazārī, al-Khwārizmī, al-Battānī, Toledan and Alfonsine Tables
III 2 5 8 10 13 16 19 21 24 27 29 11 Fātimids, Misri, or Bohra Calendar
IV 2 5 8 11 13 16 19 21 24 27 30 9 Habash al-Hāsib, al-Bīrūnī, Elias of Nisibis

The number of leap years between the beginning of year 0 and the beginning of year $$j$$ is

\begin{equation} N = \floorratio{11j + r}{30} \end{equation}

There are two variants for each pattern shift: the astronomical epoch "a" according to which the first day of the calendar (1 Muharram of year 1) began on the evening of Wednesday 14 July 622 (approximately CJD 1948438.75), and the civil epoch "c" according to which the calendar began on the evening of Thursday 15 July 622 (approximately CJD 1948439.75). The most commonly used administrative calendar is IIc.

For the lowest calendar level we seek a straight line that alternates between 30 and 29 days except that the 12th month has 30 days, and for short years (with 354 days) we cut away the last day. In a calendar based on a straight line for 355/12, the alternation between 30 and 29 days is already broken after 5 or 7 months, but we need that alternation to continue for at least 11 months. It turns out that we need a $$p$$ for that of at most 29 + 6/11, and 355/12 = 29 + 7/12 is greater than that. We use $$p = 29 \frac{6}{11} = 325/11$$.

In the Islamic Calendar the calendar days run from sunset to sunset. The CJDN calendar days run from midnight to midnight. For the calculations, we make the connection at noon. The CJDN that according to the following formulas is connected with a particular Islamic calendar date, actually corresponds to that Islamic calendar date only between midnight and sunset. Between sunset and midnight, the next calendar day has already begun according to the Islamic calendar, but not yet according to the CJDN.

For example, CJDN 2455774 corresponds to Gregorian date 31 July 2011 (31-07-2011) and runs from midnight to midnight local time. The below formulas say (for type IIc) that CJDN 2455774 corresponds to Islamic date 29 Shaban 1432 (29-08-1432) ― this means that those two dates correspond to each other between midnight and sunset. From sunset to midnight, it is still 31 July in the Gregorian calendar and still CJDN 2455774, but in the Islamic calendar it is already the next day (i.e., 1 Ramaḍān).

 Type $${r}$$ $${J_0}$$ Ia 15 1948439 Ic 15 1948440 IIa 14 1948439 IIc 14 1948440 IIIa 11 1948439 IIIc 11 1948440 IVa 9 1948439 IVc 9 1948440

\begin{align} x_2 \| = j − 1 \\ x_1 \| = m − 1 \\ z_1 \| = d − 1 \\ c_1 \| = \floorratio{325x_1 + 5}{11} \\ y_1 \| = c_1 + z_1 \\ z_2 \| = y_1 \\ c_2 \| = \floorratio{10631x_2 + r}{30} \\ y_2 \| = c_2 + z_2 \\ J \| = y_2 + J_0 \end{align}

For example, which CJDN corresponds to the Islamic date of 29 Shaban 1432 according to calendar type IIc? Then

\begin{align*} j \| = 1432 \\ m \| = 8 \\ d \| = 29 \\ r \| = 14 \\ J_0 \| = 1948440 \\ x_2 \| = 1432 − 1 = 1431 \\ x_1 \| = 8 − 1 = 7 \\ z_1 \| = 29 − 1 = 28 \\ c_1 \| = \floorratio{325×7 + 5}{11} = \floorratio{2280}{11} = 207 \\ y_1 \| = 207 + 28 = 235 \\ z_2 \| = 235 \\ c_2 \| = \floorratio{10631×1431 + 14}{30} = \floorratio{15212975}{30} = 507099 \\ y_2 \| = 507099 + 235 = 507334 \\ J \| = 507334 + 1948440 = 2455774 \end{align*}

This can be condensed to

\begin{equation} J = \floorratio{10631j − 10631 + r}{30} + \floorratio{325m − 320}{11} + d − 1 + J_0 \end{equation}

i.e.,

 Ia $${J = \floorratio{10631j − 10616}{30} + \floorratio{325m − 320}{11} + d + 1948438}$$ Ic $${J = \floorratio{10631j − 10616}{30} + \floorratio{325m − 320}{11} + d + 1948439}$$ IIa $${J = \floorratio{10631j − 10617}{30} + \floorratio{325m − 320}{11} + d + 1948438}$$ IIc $${J = \floorratio{10631j − 10617}{30} + \floorratio{325m − 320}{11} + d + 1948439}$$ IIIa $${J = \floorratio{10631j − 10620}{30} + \floorratio{325m − 320}{11} + d + 1948438}$$ IIIc $${J = \floorratio{10631j − 10620}{30} + \floorratio{325m − 320}{11} + d + 1948439}$$ IVa $${J = \floorratio{10631j − 10622}{30} + \floorratio{325m − 320}{11} + d + 1948438}$$ IVc $${J = \floorratio{10631j − 10622}{30} + \floorratio{325m − 320}{11} + d + 1948439}$$

For the same Islamic date as before we now find

\begin{align*} J \| = \floorratio{10631×1432 − 10617}{30} + \floorratio{325×8 − 320}{11} + 29 + 1948439 \\ \| = \floorratio{15212975}{30} + \floorratio{2280}{11} + 29 + 1948439 \\ \| = 507099 + 207 + 29 + 1948439 = 2455774 \end{align*}

which is the same answer as before.

#### 13.8.2. From CJDN to Islamic Date

In the opposite direction we find:

\begin{align} y_2 \| = J − J_0 \\ \{x_2, r_2\} \| = \Div(30y_2 + 29 − r, 10631) \\ z_2 \| = \floorratio{r_2}{30} \\ y_1 \| = z_2 \\ \{x_1, r_1\} \| = \Div(11y_1 + 5, 325) \\ z_1 \| = \floorratio{r_1}{11} \\ j \| = x_2 + 1 \\ m \| = x_1 + 1 \\ d \| = z_1 + 1 \end{align}

For example, which Islamic date (calendar type IIc) corresponds to CJDN 2455774? Then $$J = 2455774$$, $$r = 14$$, $$J_0 = 1948440$$, and then

\begin{align*} y_2 \| = 2455774 − 1948440 = 507334 \\ \{x_2, r_2\} \| = \Div(30×507334 + 15, 10631) = \Div(15220035, 10631) = \{1431, 7074\} \\ z_2 \| = \floorratio{7074}{30} = 235 \\ y_1 \| = 235 \\ \{x_1, r_1\} \| = \Div(11×235 + 5, 325) = \Div(2590, 325) = \{7, 315\} \\ z_1 \| = \floorratio{315}{11} = 28 \\ j \| = 1432 \\ m \| = 8 \\ d \| = 29 \end{align*}

which means 29 Shaban 1432.

This can be condensed to

\begin{align} \{x_2, r_2\} \| = \Div(30(J − J_0) + 29 − r, 10631) \\ \{x_1, r_1\} \| = \Div\left( 11\floorratio{r_2}{30} + 5, 325 \right) \\ j \| = x_2 + 1 \\ m \| = x_1 + 1 \\ d \| = \floorratio{r_1}{11} + 1 \end{align}

For example, which Islamic date (calendar type IIc) corresponds to CJDN 2455774? Then $$J = 2455774$$, $$r = 14$$, $$J_0 = 1948440$$, and then

\begin{align*} \{x_2, r_2\} \| = \Div(30×(2455774 − 1948440) + 29 − 14, 10631) \\ \| = \Div(15220035, 10631) = \{1431, 7074\} \\ \{x_1, r_1\} \| = \Div\left( 11×\floorratio{7074}{30} + 5, 325 \right) \\ \| = \Div(11×235 + 5, 325) = \Div(2590, 325) = \{7, 315\} \\ j \| = 1431 + 1 = 1432 \\ m \| = 7 + 1 = 8 \\ d \| = \floorratio{315}{11} + 1 = 29 \end{align*}

which means 29 Shaban 1432, which is the same as in the previous example.

### 13.9. The Maya Calendar

#### 13.9.1. Between CJDN and Maya Calendar

The Maya from Anahuac (Central America) used three different calendars, of which two (the Tzolkin and Haab) were periodic with fairly short periods, and the third one (the Long Count) might have been intended to be periodic but has some periods that are so long that it can be considered to be continuous rather than repeating.

Different areas in Central America had slightly different versions of these calendars, with different names for days and months, and different ways to indicate years, and sometimes days were counted from 0 rather than from 1. Below we describe the calendars from the city of Tikal.

#### 13.9.2. The Haab

The Haab has a day number and a month, but no year number. There are 18 months of 20 days, plus a 19th month of 5 days, which makes 365 days in total, which we call a Haab year. There are no leap years. The months have a name, and the days have a number beginning at 0.

We write a date in the Haab as $$\{h_d,h_m\}$$ where $$h_d$$ is the day number (from 0 through 19) and $$h_m$$ is the month number (from 1 through 19).

To translate a CJDN $$J$$ to a Haab date $$\{h_d,h_m\}$$ we must first calculate $$H$$, the number of days since the beginning of the current Haab year (with $$H = 0$$ for the first day of the Haab year).

\begin{align} H \| = (J + 65) \bmod 365 \\ h_m \| = \floorratio{H}{20} + 1 \\ h_d \| = H \bmod 20 \end{align}

For example, which Haab date corresponds to 15 December 1965 = CJDN 2439110? Then $$J = 2439110$$ and

\begin{align*} H \| = (J + 65) \bmod 365 = 2439175 \bmod 365 = 245 \\ h_m \| = \floorratio{245}{20} + 1 = 13 \\ h_d \| = 245 \bmod 20 = 5 \end{align*}

so that day is 245 days since the beginning of the current Haab year and corresponds to the 5th day of the 13th month, i.e., $$\{5.13\}$$.

To translate a Haab date $$\{h_d,h_m\}$$ to a CJDN we first calculate $$H$$.

\begin{equation} H = h_d + 20×(h_m − 1) \end{equation}

We know that

\begin{equation} J ≡ H − 65 \pmod{365} \end{equation}

Because there is no year number in the Haab, this Haab date returns every 365 days. We can use equation \eqref{eq:laatste} to find the last $$J$$ that corresponds to this Haab date, on or before a specific $$J_0$$:

\begin{equation} J = J_0 − ((J_0 − H + 65) \bmod 365) \end{equation}

Which CJDN corresponds to Haab date $$\{5,13\}$$? Then $$h_d = 5$$, $$h_m = 13$$, so $$H = 5 + 20×(13 − 1) = 245$$ and then $$J ≡ 245 − 65 ≡ 180 \pmod{365}$$. That fits the original $$J$$ that we began with in the previous example (2439110), because $$2439110 = 6682×365 + 180 ≡ 180 \pmod{365}$$.

What is the last CJDN in the year 1965 that corresponds to Haab date $$\{5,13\}$$? We seek the last $$J ≡ 180 \pmod{365}$$ on or before CJDN $$J_0 = 2439126$$ (which corresponds to 31 December 1965), using equation \eqref{eq:laatste}. We find

\begin{align*} J \| = J_0 − ((J_0 − 180) \bmod 365) \\ \| = 2439126 − (2438946 \bmod 365) \\ \| = 2439126 − 16 = 2439110 \end{align*}

#### 13.9.3. The Tzolkin

The Tzolkin has a period of 20 days (the venteina) with a name for each day, and a period of 13 days (the trecena) with a number for each day (beginning at 1).

We write a date in the Tzolkin as $$\{t_t,t_v\}$$, where $$t_t$$ is the day number in the trecena (from 1 through 13) and $$t_v$$ is the day number in the venteina (from 1 through 20).

The trecena and venteina increase simultaneously, so after day $$\{4,7\}$$ comes day $$\{5,8\}$$, and then $$\{6,9\}$$, and so on. Because 13 and 20 are relatively prime, all possible combinations of venteina and trecena occur in this calendar, so after 13×20 = 260 days the dates repeat again.

We translate CJDN $$J$$ to a Tzolkin date as follows:

\begin{align} t_t \| = ((J + 5) \bmod{13}) + 1 \label{eq:trecena2} \\ t_v \| = ((J + 16) \bmod{20}) + 1 \label{eq:venteina2} \end{align}

What is the Tzolkin date corresponding to CJDN 2439110? Then

\begin{align*} t_t \| = ((2439110 + 5) \bmod 13) + 1 = 3 + 1 = 4 \\ t_v \| = ((2439110 + 16) \bmod 20) + 1 = 6 + 1 = 7 \end{align*}

so that date is $$\{4,7\}$$.

To translate a Tzolkin date $$\{t_t,t_v\}$$ to a CJDN we need to solve the two simultaneous congruences of equations \eqref{eq:trecena2} and \eqref{eq:venteina2}, i.e.,

\begin{align} J \| ≡ t_t − 6 \pmod{13} \\ J \| ≡ t_v − 17 \pmod{20} \end{align}

We combine those congruences in the way of section 12.10.3. We have $$x_1 = t_t$$, $$x_2 = t_v$$, $$p_1 = 13$$, $$p_2 = 20$$, $$a_1 = 6$$, $$a_2 = 17$$. Then

\begin{align} C_1 \| = x_1 − a_1 = t_t − 6 \\ P_1 \| = p_1 = 13 \end{align}

The greatest common divisor of 13 and 20 is 1:

\begin{align} g_2 \| = 1 \\ Q_1 \| = \frac{P_1}{g_2} = \frac{13}{1} = 13 \\ q_2 \| = \frac{p_2}{g_2} = \frac{20}{1} = 20 \end{align}

We now seek a $$r_2$$ that solves $$r_2Q_1 ≡ 1 \pmod{q_2}$$, i.e., $$13r_2 ≡ 1 \pmod{20}$$, and find $$r_2 = 17$$, because $$13×17 = 221 ≡ 1 \pmod{20}$$.

\begin{align} \| \begin{split} C_2 \| = C_1 (1 − r_2Q_1) + (x_2 − a_2) r_2Q_1 \\ \| = (t_t − 6)×(1 − 17×13) + (t_v − 17)×17×13 \\ \| = 221 t_v − 220 t_t − 2437 \end{split} \\ P_2 \| = p_2Q_1 = 20×13 = 260 \end{align}

so the solution is

\begin{equation} J ≡ 221 t_v − 220 t_t − 2437 = −39 t_v + 40 t_t − 97 \pmod{260} \end{equation}

Which days correspond to Tzolkin date $$\{4,7\}$$? Those days have $$J ≡ −39×7 + 40×4 − 97 ≡ 50 \pmod{260}$$. That fits with $$J = 2439110$$ because $$2439110 = 9381×260 + 50 ≡ 50 \pmod{260}$$.

Because there is no year number in the Tzolkin, this Tzolkin date returns every 260 days. We can use equation \eqref{eq:laatste} to find the last $$J$$ on or before a particular $$J_0$$ that corresponds with this Tzolkin date:

\begin{equation} J = J_0 − ((J_0 − 40 t_t + 39 t_v + 97) \bmod 260) \end{equation}

The last Tzolkin date $$\{4,7\}$$ that occurs on or before 31 December 1965 (= CJDN 2439126) is $$J = 2439126 − ((2439126 − 50) \bmod 260) = 2439126 − 16 = 2439110$$.

For the number of days $$T$$ since the last $$\{1,1\}$$ we have

\begin{equation} \begin{split} T \| ≡ 40 (t_t − 1) − 39 (t_v − 1) \\ \| ≡ 40 t_t − 39 t_v − 1 \\ \| ≡ 40 t_t + 221 t_v − 1 \pmod{260} \end{split} \end{equation}

so

\begin{equation} T ≡ (40 t_t + 221 t_v − 1) \bmod 260 \end{equation}

For CJDN 2439110 (with $$\{t_t,t_v\} = \{4,7\}$$) we find $$T = (40×4 + 221×7 − 1) \bmod 260 = 1706 \bmod 260 = 146$$, so the last day before then that had $$\{1,1\}$$ was CJDN $$J = 2439110 − 146 = 2438964$$, and that fits, because $$t_t = ((J + 5) \bmod 13) + 1 = (2438969 \bmod 13) + 1 = 1$$ and $$t_v = ((J + 16) \bmod 20) + 1 = (2438980 \bmod 20) + 1 = 1$$.

#### 13.9.4. Tzolkin and Haab

Sometimes a date is indicated in both Tzolkin and Haab. From CJDN to Tzolkin and Haab is described above. From a date with Tzolkin and Haab to CJDN is a bit more complicated. We need to solve simultaneously:

\begin{align} J \| ≡ H − 65 \pmod{365} \\ J \| ≡ T − 96 \pmod{260} \end{align}

We found the first formula for the Haab alone, and the second formula for the Tzolkin alone. These formulas again have the form from section 12.10.3, with $$x_1 = H$$, $$x_2 = T$$, $$p_1 = 365$$, $$p_2 = 260$$, $$a_1 = 65$$, $$a_2 = 96$$. Then

\begin{align} C_1 \| = x_1 − a_1 = H − 65 \\ P_1 \| = p_1 = 365 \end{align}

The greatest common divisor of 365 and 260 is 5, so

\begin{align} g_2 \| = 5 \\ Q_1 \| = \frac{P_1}{g_1} = \frac{365}{5} = 73 \\ q_2 \| = \frac{p_2}{g_1} = \frac{260}{5} = 52 \end{align}

We now seek $$r_2$$ that satisfies $$r_2Q_1 ≡ 1 \pmod{q_2}$$, i.e., $$73r_2 ≡ 1 \pmod{52}$$, and find $$r_2 = 5$$, because $$73×5 = 365 = 7×52 + 1 ≡ 1 \pmod{52}$$. Then

\begin{align} \| \begin{split} C_2 \| = C_1 (1 − r_2Q_1) + (x_2 − a_2) r_2Q_1 \\ \| = (H − 65)×(1 − 5×73) + (T − 96)×5×73 \\ \| = 365 T − 364 H − 11380 \end{split} \\ P_2 \| = p_2Q_1 = 365×52 = 18980 \end{align}

so the solution is

\begin{equation} J ≡ 365 T − 364 H − 11380 ≡ 365 T − 364 H + 7600 \pmod{18980} \end{equation}

The Tzolkin and Haab together have a period of 18980 days, which is approximately 52 years.

Which CJDN corresponds to $$\{t_t,t_v,h_d,h_m\} = \{4,7,5,13\}$$? Then $$H = 245$$ and $$T = 146$$, so $$J ≡ 365×146 − 364×245 + 7600 ≡ −28290 ≡ 9670 \pmod{18980}$$, and that fits with the original $$J = 2439110$$ that we began with, because $$2439110 = 128×18980 + 9670 ≡ 9670 \pmod{18980}$$.

Because $$g_2$$ is not equal to 1, not all possible combinations of $$H$$ and $$T$$ occur in the calendar. The preceding formula yields a $$J$$ for every combination of $$H$$ and $$T$$, but that $$J$$ is only useful for combinations of $$H$$ and $$T$$ that actually occur in the calendar. Those are combinations for which $$H − T ≡ 65 − 96 ≡ 4 \pmod{5}$$.

In the example that we've been using we have $$H = 245$$ and $$T = 146$$, so $$H − T = 99 ≡ 4 \pmod{5}$$, so this combination indeed occurs in this calendar.

#### 13.9.5. The Long Count

The Long Count counts days and has a series of increasingly longer periods. The smallest period contains 20 days, the next one is 18 times longer (i.e., 360 days), and after that each next period is 20 times longer than the previous one. The number for each period begins at 0. Often dates in the Long Count are given with five numbers, but even longer periods are known, up to nine numbers. The longest known period corresponds to about 63 million years. Here we assume that the Long Count has five numbers, and that the fifth number can get arbitrarily long.

The epoch (0.0.0.0.0) of the Long Count probably corresponds to CJDN $$J_0 = 584283$$ (6 September −3113 in the Julian calendar).

Translating CJDN $$J$$ to Long Count $$L ≡ l_5.l_4.l_3.l_2.l_1$$ goes as follows:

\begin{align} y \| = J − J_0 \\ \{l_5, r_5\} \| = \Div(y, 144000) \\ \{l_4, r_4\} \| = \Div(r_5, 7200) \\ \{l_3, r_3\} \| = \Div(r_4, 360) \\ \{l_2, l_1\} \| = \Div(r_3, 20) \end{align}

and translating a Long Count to CJDN goes as follows:

\begin{equation} \begin{split} J \| = l_1 + 20×(l_2 + 18×(l_3 + 20×(l_4 + 20×l_5))) + J_0 \\ \| = l_1 + 20×l_2 + 360×l_3 + 7200×l_4 + 144000×l_5 \end{split} \end{equation}

Which Long Count $$L$$ corresponds to CJDN $$J = 2439110$$? Then

\begin{align*} y \| = J − J_0 = 2439110 − 584283 = 1854827 \\ \{l_5, r_5\} \| = \Div(1854827, 144000) = \{12, 126827\} \\ \{l_4, r_4\} \| = \Div(126827, 7200) = \{17, 4427\} \\ \{l_3, r_3\} \| = \Div(4427, 360) = \{12, 107\} \\ \{l_2, l_1\} \| = \Div(107, 20) = \{5, 7\} \end{align*}

The answer is $$L = 12.17.12.5.7$$.

And now in the opposite direction: $$J = 7 + 20×(5 + 18×(12 + 20×(17 + 20×12))) + 584283 = 2439110$$.

### 13.10. Algorithm with a Curved Line

We are not limited to using straight lines. The method that we derived above for using a straight line can be expanded to more complicated functions. If we have functions $$v(x)$$ and $$w(y)$$ such that

\begin{align*} v'(x) \| ≥ 1 \\ v(w(x)) \| = w(v(x)) = x \end{align*}

(so $$v$$ and $$w$$ are each other's inverse functions) then the calendar formulas become

\begin{align} y \| = ⌊v(x)⌋ + z \\ x \| = ⌈w(y + 1)⌉ − 1 = −⌊−w(y + 1)⌋ − 1 \end{align}

We get the case of the straight line if we set $$v(x) = px = fx/g$$; to this corresponds $$w(y) = y/p = gy/f$$.

The derivation goes similar to the case with the straight line.

With a curved line we can make a calendar in which the average length of the month or the year does not stay constant in the long run. In this way we can keep the calendar better in step with the Sun and the Moon than for a calendar that has fixed average lengths.

A practical problem for this is that not every function $$v(x)$$ has a (reasonably simple to calculate) corresponding inverse function $$w(y)$$. You cannot use just any $$v(x)$$.

If you use a function $$v(x)$$ that (for whole numbers $$x$$) does not always yield a whole number or a ratio, or if you do the calculations with floating-point numbers, then you should worry about round-off errors. If you calculator or program makes a round-off error and yields $$v(x) = 6.9999$$ instead of $$v(x) = 7.0000$$, then you get $$⌊v(x)⌋ = 6$$ instead of $$⌊v(x)⌋ = 7$$, and so a wrong day number. To prevent round-off errors, you should pick a function $$v(x)$$ that allows calculating with ratios and that has an inverse function $$w(y)$$ that also allows calculating with ratios, so that all of the calculations can be done with whole numbers only (separately in numerators and denominators).

## 14. Validity of the Algorithms When Used in Computer Programs

The above-mentioned algorithms are in principle valid for all days, but computer programs usually work with numbers of a limited size, so in practice the algorithms won't work anymore for dates that are too far from the begin date of the calendar, or too far from CJDN 0. If the computer program works with whole numbers of $$n$$ bits wide, then the greatest number that it can handle is equal to $$w = 2^{n − 1}$$. If one or more of the intermediate results of the algorithms gets greater than $$w$$ (in positive or negative direction), then they won't fit anymore and then the program yields wrong results.

For translating a calendar date into a running day number, that running day number is usually the greatest (intermediate) result, so algorithms that translate a calendar date into a running day number will give valid results in computer programs if the running day number remains clearly less (in absolute sense) than $$w$$. The number of days since or before CJDN 0 must also remain less than $$w$$. The value of $$w$$ for oft-used number widths is shown in table 2.

$${n}$$ $${w}$$ $${w/365.25}$$
2 32 768 89
4 2 147 483 648 5.8 × 106
8 9 223 372 036 854 775 808 2.5 × 1016

For example, if the computer program uses numbers of 32 bits wide, then it can translate calendar dates into running day numbers for dates up to at least 5 million years from the beginning of the calendar (both to the future and to the past).

While translating a running day number into a calendar date, the running day number $$y$$ (since the beginning of the calendar) is entered into a formula like equation \eqref{eq:ynaarxr}, i.e., multiplied by a factor $$g$$ (a whole number greater than 1), then added to a fixed (usually small) whole number, and then divided by a divisor $$f$$ (a whole number greater than $$g$$). We can indicate that calculation schematically like this:

\begin{equation} x = \floorratio{gy + t}{f} \end{equation}

If we do the calculations in the listed order, then we get an intermediate result $$gy + t$$ which is much greater than $$y$$ itself. Intermediate results greater than $$w$$ give trouble, so to avoid trouble we'd need roughly $$|gy| \lt w$$, so $$|y| \lt w/g$$, which is much less than $$w$$ itself.

For example, the "CJDNGregorian (3)" algorithm has $$g = 4800$$, so then we'd need $$|y| \lt w/4800$$. For numbers of 32 bits wide, this algorithm would be useful as long as approximately $$|y| \lt 447392$$, i.e., for 447,392 days or 1224 years from the beginning of the calendar. This is far too small to cover the whole period since the end of prehistory.

Section 12.2 describes how we can rearrange the calculation to avoid this problem.

As an example we look at equation \eqref{eq:g3}, which we repeat here:

\begin{equation} x_2 = \floorratio{4800y_2 + 15793}{146097} \end{equation}

Then $$g = 4800$$, $$t = 15793$$, $$f = 146097$$. Suppose that $$y_2 = 730,000$$ (we calculate for a date that is 730,000 days, approximately 2000 years, after the beginning of the calendar) and $$n = 4$$ so $$w = 2^{31} = 2,147,483,648$$.

Then $$4800y_2 + 15793 = 3,504,015,793$$, which is greater than $$w$$, and $$3504015793/146097 = 23984 + 25345/146097$$, so $$x_2 = 23984$$. If we calculate $$x_2$$ in this way then we get an intermediate result that is too great to fit into a number of 32 bits wide.

Now we use the alternative method. Then

$x_2 = 4800\floorratio{y_2}{146097} + \floorratio{4800(y_2 \bmod 146097) + 15793}{146097}$

We find

\begin{align*} \floorratio{y_2}{146097} \| = \floorratio{730000}{146097} = 4 \\ y_2 \bmod 146097 \| = y_2 − 146097\floorratio{y_2}{146097} = 145612 \\ x_2 \| = 4800×4 + \floorratio{4800×145612 + 15793}{146097} \\ \| = 19200 + \floorratio{698953393}{146097} = 19200 + 4784 = 23984 \end{align*}

This gives the same end result as above, but now the greatest intermediate result was 698,953,393, which is less than $$w$$.

With this alternative method, the greatest possible intermediate result for this calendar is equal to $$η = fg − f + t = f×(g − 1) + t = 4799×146097 + 15793 = 701,135,296$$, which fits into a number of 32 bits wide.

With this alternative we can, by doing more calculations, translate running day numbers into calendar dates for about the same range of dates as the translation of calendar dates into running day numbers, if $$fg − f + t \lt w$$.

Table 3 shows, for all algorithms described above, the relevant value of $$g$$ and (approximately) the greatest allowed value of $$y$$ (measured in days and years) for $$n$$ equal to 16, 32, and 64 ― the width in bits of the most commonly used types of whole numbers in computer programs. This shows the limitations of the use of the algorithms if the alternative way of calculation that was discussed above is not used. The last columns shown $$t$$, $$f$$, the greatest possible intermediate result $$η$$ (apart from $$y$$) if the alternative method is used, and the minimum width $$n_{min}$$ in bits that is required for the alternative method to work well in the computer program.

Calendar $${g}$$ $${y}$$ $${y/365.25}$$ $${t}$$ $${f}$$ $${η}$$ $${n_{min}}$$
16 32 64 16 32 64
CJDN → Julian (1) 4 8192 536 × 106 2305 × 1015 22 1469872 6313 × 1012 3 1461 4386 2
CJDN → Julian (2) 48 682 44 × 106 192 × 1015 1 122489 526 × 1012 109 1461 68776 3
CJDNGregorian (1) 4 8192 536 × 106 2305 × 1015 22 1469872 6313 × 1012 3 146097 438294 2
CJDNGregorian (2) 400 81 5 × 106 23 × 1015 0 14698 63 × 1012 799 146097 58293502 4
CJDNGregorian (3) 4800 6 447392 1.9 × 1015 0 1224 5 × 1012 15793 146097 701135296 4
CJDN → Babylonian (1) 235 139 9 × 106 39 × 1015 0 25019 10 × 1012 234 6940 1624194 3
CJDN → Babylonian (2) 19 1724 113 × 106 485 × 1015 4 309446 1329 × 1012 546 6940 125466 3
CJDN → Islamic 30 1092 71 × 106 307 × 1015 3 195983 841 × 1012 15 10631 308314 3

The "CJDNGregorian (3)" algorithm is, for whole numbers of 32 bits wide, valid for dates up to 1224 years from the beginning of the calendar. If you use that algorithm with that number width for dates near today, then you'll get wrong answers. How the answers are wrong depends on what your computer does with intermediate results that don't fit, and that can be different for each computer. If you want to use this algorithm in practice, then your computer program should either use whole numbers of 64 bits wide (then the algorithm is valid for about 5,000,000,000,000 years), or use whole numbers of 32 bits wide and also use the alternative method explained above (then the algorithm is valid for about 6,000,000 years).

## 15. General Algorithms for Calculating Between Calendar Date and Running Day Number

### 15.1. From Calendar Date to Running Day Number

Below is an algorithm (in pseudo-code) for calculating a running day number from a calendar date. The meaning of the various function arguments is:

date[i,j] is a two-dimensional table, in which i selects the different calendar units (day, month, and so on; there are n + 1 of them) and j selects the different dates (there are t of them).

q[i] is the q (base period) of calendar level i.

g[i] is the g (denominator) of calendar level i.

b[i] indicates the calendar type (1 through 4) of calendar level i.

d[i][k] is the d (deviation from the base period) of period k (there are m of these) of calendar level i.

h[i][k] is the h (numerator) of period k of calendar level i.

s[i][k] is the s (pattern shift) of period k of calendar level i.

y[j] is the array of running day numbers.

The function DIV(x,y) corresponds to ⌊x/y⌋. The function MOD(x,y) corresponds to x mod y = x − y*⌊x/y⌋.

Here is the algorithm:

function CAL2DAY(date, q, g, b, d, h, s)
for j from 0 through t − 1:
set y[j] to date[0,j]
for i from 0 through n − 1:
if MOD(b[i],2) = 1 then:
set z[j] to y[j]
set x[j] to date[i+1,j]
else:
set z[j] to date[i+1,j]
set x[j] to y[j]
set c[j] to q[i]*x[j]
for k from 0 through m − 1:
set c[j] to c[j] + MOD(h[i][k]*x[k,j] + s[i][k],g[i])*d[i][k]
set y[j] to c[j] + z[j]
return y
end function


### 15.2. From Running Day Number to Calendar Date

Here is a general algorithm (in pseudo-code) for calculating a calendar date from a running day number for all four calendar types, in similar fashion as above.

function DAY2CAL(day, q, g, b, d, h, s)
for i from 0 through n − 1:
set dp[i] to 0
set s2[i] to 0
set f[i] to q[i]*g[i]
for k from 0 through m − 1:
set f[i] to f[i] + d[i][k]*h[i][k]
if d[i][k] gt 0 then:
set dp[i] to dp[i] + d[i][k]
set s2[i] to d[i][k]*s[i][k]
set s2[i] to g[i]*dp[i] − s2[i] − 1
set zz[i] to q[i] + dp[i]
for j from 0 through t − 1:
set y[j] to day[j]
for i from n − 1 through 0:
set x[j] to DIV(g[i]*y[j] + s2[i],f[i])
set c[j] to q[i]*x[j]
for k from 0 through m − 1:
set c[j] to c[j] + DIV(h[i][k]*x[j] + s[i][k],g[i])*d[i,k]
set z[j] to y[j] − c[j]
if b[i] gt 2 then:
while z[j] lt 0:
set x[j] to x[j] + DIV(z[j],zz[i])
set c[j] to q[i]*x[j]
for k from 0 through m − 1:
set c[j] to c[j] + DIV(h[i][k]*x[j] + s[i][k],g[i])*d[i,k]
set z[j] to y[j] − c[j]
if MOD(b[i],2) = 1 then:
set y[j] = z[j]
set date[i + 1,j] = x[j]
else:
set date[i + 1,j] = z[j]
set y[j] = x[j]
set date[0,j] = y[j]
return date
end function


### 15.3. Parameters for Various Calendars

The below table shows for each of the calendars which calendar units in which order should be provided to CAL2DAY as date, or that are returned by DAY2CAL. d, m, y, c indicate day, month, year, and century, respectively. A * indicates the use of calculation months/years/centuries where the calculation year begins with the month of March.

Calendar Order
Julian (1) dm*y*
Julian (2) ymd
Gregorian (1) dm*y*c*
Gregorian (2) dm*y*
Gregorian (3) ymd
Babylonian (1) ymd
Babylonian (2) dmy
Islamic dmy

The parameters that are needed to use the above algorithms for the various calendars are shown in the following table.

Julian Gregorian Babylonian Islamic
(1) (2) (1) (2) (3) (1) (2)
J₀1721117.5 1721057.5 1721119.5 1721119.5 1721059.5 1607557.6 1607557.5 1948440
q₁, g₁, b₁30, 5, 1 12, 1, 4 30, 5, 1 30, 5, 3 12, 1, 4 12, 19, 2 29, 13, 1 29, 11, 1
d, h, s1, 3, 2 1, 0, 0 1, 3, 2 1, 3, 2 1, 0, 0 1, 7, 13 1, 7, 3 1, 6, 5
q₂, g₂, b₂365, 4, 1 30, 48, 4 365, 100, 1 365, 400, 3 30, 4800, 4 29, 235, 2 354, 19, 3 354, 30, 1
d, h, s1, 1, 0 1, 28, 20 1, 25, 0 1, 100, 0 1, 2800, 2000 1, 125, 0 30, 7, 1 1, 11, 14
−2, 4, 40 −1, 4, 0 −2, 400, 4000 1, 4, 12
1, 1, 46 1, 1, 0 1, 100, 4600
−1, 4, 4792
1, 1, 4798
q₃, g₃, b₃ 36524, 4, 1
d, h, s 1, 1, 0

### 15.4. Julian Calendar

If you want to use this function for the Julian (1) calendar, then you should use the following function arguments:

1. q = [30, 365]
2. g = [5, 4]
3. b = [1, 1]
4. d = [1, 1]
5. h = [3, 1]
6. s = [2, 0]

q should contain the following elements: [ [ 1461, 4, 0 ], [ 153, 5, 4 ] ]. x[0,j] is then the calculation year number, x[1,j] is the month number within the calculation year (March is month number 0!), and x[2,j] is the day number within the month (the first day has day number 0!).

If you want to use the function for the Gregorian calendar, then q should contain: [ [ 146097, 4, 0 ], [ 36525, 100, 0 ], [ 153, 5, 4 ] ]. Then x[0,j] is the calculation century number, x[1,j] the calculation year within the calculation century, x[2,j] the month number within the calculation year (March has month number 0!), and x[3,j] the day number within the month (the first day has number 0!).

Here is pseudocode to calculate a CJDN from a Julian calendar date x:

function JUL2CJDN(x)
set q to [ [ 1461, 4, 0 ], [ 153, 5, 4 ] ]
for i from 0 through n−1:
set x[1,i] to x[1,i] − 3
set x[2,i] to x[2,i] − 1
if x[1,i] is less than 0:
set x[1,i] to x[1,i] + 12
set x[0,i] to x[0,i] − 1
set d to CAL2DAY(x,q)
for j from 0 through t−1:
set y[j] to y[j] + 1721118
return y
end function


Here is pseudocode to calculate a CJDN from a Gregorian calendar date c:

function GREG2CJDN(x)
set q to [ [ 146097, 4, 0 ], [ 36525, 100, 0 ], [ 153, 5, 4 ] ]
for i from 0 through n−1:
set x[1,i] to x[1,i] − 3
if x[1,i] is less than 0:
set x[1,i] to x[1,i] + 12
set x[0,i] to x[0,i] − 1
set b[0,i] to DIV[x[0,i],100]
set b[1,i] to x[0,i] − 100*b[0,i]
set b[2,i] to x[1,i]
set b[3,i] to x[2,i] − 1
set d to CAL2DAY(b,q)
for j from 0 through t−1:
set y[j] to y[j] + 1721120
return y
end function


Here is pseudocode to calculate Julian calendar dates from CJDN y:

function CJDN2JUL(y)
set q to [ [ 1461, 4, 0 ], [ 153, 5, 4 ] ]
for j from 0 through t−1:
set y[j] to y[j] − 1721118
set c to DAY2CAL(y,q)
for i from 0 through n−1:
set x[1,i] to x[1,i] + 3
set x[2,i] to x[2,i] + 1
if x[1,i] is greater than 12:
set x[1,i] to x[1,i] − 12
set x[0,i] to x[0,i] + 1
return x
end function


And here is pseudocode to calculate Gregorian calendar dates from CJDN y:

function CJDN2GREG(y)
set q to [ [ 146097, 4, 0 ], [ 36525, 100, 0 ], [ 153, 5, 4 ] ]
for j from 0 through t−1:
set y[j] to y[j] − 1721120
set b to DAY2CAL(y,q)
for i from 0 through n−1:
set x[0,i] to 100*b[0,i] + b[1,i]
set x[1,i] to b[2,i] + 3
set x[2,i] to b[3,i] + 1
if x[1,i] is greater than 12:
set x[1,i] to x[1,i] − 12
set x[0,i] to x[0,i] + 1
return x
end function
`

## 16. Fast Estimate of the Number of Days Between Two Dates

If you want to know how many days there are between two dates, then you can calculate that exactly by subtracting the CJDNs of the two dates. If you are satisfied with a possible error of a few days, then you can also estimate it from the difference measured in calendar years, calendar months, and calendar days.

Such information (the number of calendar years, months, and days) is not sufficient to calculate the exact number of days for all calendars, because not every year contains the same number of days as every other year, and not every month contains the same number of days as every other month (in all calendars).

Let's assume that the second date is $$j$$ calendar years and $$m$$ calendar months and $$d$$ calendar days later than the first date (both in the same calendar, the Gregorian Calendar or the Jewish Calendar or the administrative Islamic Calendar), where you just subtract the year number, month number, and day number of the second date from those of the first date (which must be later). The variable $$j$$ may be positive or zero, but not negative. The variables $$m$$ and $$d$$ may be positive or zero or negative, but $$m$$ must not correspond to more than one calendar year, and $$d$$ must not correspond to more than one calendar month. For the Jewish calendar we define Tishri to be month number 1 and define Adar Ⅱ to belong to month number 6 together with Adar Ⅰ (so that month number 6 may contain up to 59 days). For the Egyptian calendar we count the last 5 days of the year as a 13th month. Then you can estimate the number $$n$$ of days between those two dates using the formula

\begin{equation} n = ⌊a j + b m + c d⌋ \end{equation}

with $$a$$, $$b$$, and $$c$$ from the following table:

$${a}$$ $${b}$$ $${c}$$ $${|Δ|_\text{max}}$$ $${σ}$$ $${p_0}$$
Gregorian 365.24 30.4 1 4 1.1 35 %
Jewish 365.25 31.0 0.9 37 12.9 3 %
Islamic 354.367 29.5 1 2 0.6 62 %
Egyptian 365 30 1 0 0 100 %

The $$|Δ|_\text{max}$$ from the table shows the greatest error (measured in days) that the formula for that calendar yields (based on 10,000 random test dates) for dates that are not more than 400 years apart. The $$σ$$ shows the standard deviation (say, the average error, also measured in days). The $$p_0$$ shows the probability that the estimated number $$n$$ of days between the two dates is exactly right.

It is clear that the Egyptian calendar is the most predictable one, because it yields the least mistakes (namely zero). Then comes the administrative Islamic calendar, then the Gregorian calendar (with more variation in the month lengths than the Islamic calendar has), en lastly the Jewish calendar (with variation in the number of months in the year).

An example: How many days are there between 2003-05-25 and 2017-01-17 in the three calendars? Those dates differ by 14 years, −4 months, and −8 days. In the Gregorian calendar this yields $$n = ⌊365.24×14 + 30.4×−4 − 8⌋ = ⌊4983.76⌋ = 4983$$ days. The real number of days between those two dates is 4986.

In the Jewish calendar this yields $$n = ⌊365.25×14 + 31.0×−4 + 0.9×−8⌋ = ⌊4982.3⌋ = 4983$$ days, which happens to be equal to the real number of days between those two dates in the Jewish calendar.

In the administrative Islamic calendar this yields $$n = ⌊354.367×14 + 29.5×−4 − 8⌋ = ⌊4835.138⌋ = 4835$$ days, which is equal to the real number of days between those two dates in the administrative Islamic calendar.

In the Egyptian calendar this yields $$n = ⌊365×14 + 30×−4 − 8⌋ = ⌊4982⌋ = 4982$$ days, which is equal to the real number of days between those two dates in the Egyptian calendar. languages: [en] [nl]

//aa.quae.nl/en/reken/juliaansedag.html;
Last updated: 2022-03-16