# Algorithm explanation

1. Aug 7, 2007

### lewis198

Hi guys, I have another weak problem here if you would care to help, please.

I am trying to convert between the julian day and the gregorian date, and I found a eally good website:http://www.ortelius.de/kalender/calc_en.php [Broken]

The only problem is that when I run into the centennial cycle another factor comes into play. Here is the text:

Computing the Gregorian date of a given JD

The Gregorian calendar is based on a 400 year cycle, after which the days of the week and the sequence of leap years repeat. Each 400 year cycle consists of four 100 year cycles of which the first three end with a common year, while the last cycle ends with a leap year. Every 100 year cycle contains 4 year cycles, of which the first three years are common years followed by a leap year. The irregular and unequally distributed month lengths make it difficult to find an algorithm with only few exceptions.

The formulas are derived according to a relatively simple pattern. First, the JD is increased by 32044. The result N1 ist the number of elapsed days since 1 March 4801 BCE, Gregorian. The division of this number N2 by 146097 days gives the number of elapsed 400 year cycles, each of them containing 146097 days. The remainder of this division (N3) shows, how many days of the current 400 year cycle have elapsed already. Dividing N3 by 36524 days (= 100 years) gives us the number of the elapsed 100 year cycles within the current 400 year cycle. The last day of the last 100 year cycle, though, has to be treated exceptionally, this day being counted as the first day of a fifth 100 year cycle of the current 400 year cycle. This exception is made by simply taking the minimum of 3 and the result of the foregoing division. The last year of the last 100 year cycle is a leap year, which makes it one day longer than the last years of the first three 100 year cycles of the current 400 year cycle. (E. g., 29 February 2000 is such an exceptional day.) For the same reason, one cannot simply take the remainder of the division to compute the elapsed days within the current 100 year cycle, this remainder becoming 0 in case of the "last day", see the equation for the calculation of N4 and N5.

A four year cycle ends with a leap year, which is one day longer than each of the three preceeding common years. Therefore, the computation of the number of elapsed years within a four year cycle (N8) and of the number of elapsed days within the current year (N9) is done the same way.

N6 is the number of elapsed 4 year cycles within the current 100 year cycle. The last 4 year cycle of each 100 year cycle normally is one day shorter than the other four year cycles, making similar exceptions as mentioned above unnecessary.

The number of the day within the current year is N9. Note, that "year" means "adjusted year", beginning with the third month March and ending with the fourteenth month February. The computation of the number of the month (N10) from N9 is done with a formula derived empirically by dividing N9 by the mean length of a month and some trying.

The result of all this are the following equations.

N1 = JD + 32044
N2 = int(N1 / 146097)
N3 = N1 mod 146097
N4 = min(3 ; int(N3 / 36524))
N5 = N3 - 36524 · N4
N6 = int(N5 / 1461)
N7 = N5 mod 1461
N8 = min(3 ; int(N7 / 365))
N9 = N7 - 365 · N8
N10 = int((111 · N9 + 41) / 3395)
D = N9 - 30 · N10 - int((7 · (N10 + 1)) / 12) + 1
M' = N10 + 3
Y' = 400 · N2 + 100 · N4 + 4 · N6 + N8 - 4800
M = ((M' + 11) mod 12) + 1
Y = Y' + int(M' / 13)

The determination of the minimum of two numbers can be put in a more formal way. The formulas for N4 and N8 can be substituted by

N4 = int(N3 / 36524) - int(N3 / 146096)
N8 = int(N7 / 365) - int(N7 / 1460)

The underlined part is what I don't understand: what on earth does that min(3 stuff mean? and what would happen if you don't use it?