Astronomy Answers: Julian Day Number

Astronomy Answers
Julian Day Number


[Astronomy Answers] [Dictionary] [AnswerBook] [Universe Family Tree] [Science] [Starry Sky] [Planet Positions] [Calculate] [Colophon]

1. Rounding ... 2. From Gregorian Date to Julian Day Number ... 3. From Julian Day Number to Gregorian Date ... 4. From Julian Date to Julian Day Number ... 5. From Julian Day Number to Julian Date ... 6. Derivation of the Algorithms ... 6.1. Decomposition into Periods ... 6.2. Algorithm with the Straight Line ... 6.3. Combinations of straight lines ... 6.3.1. Flat combination ... 6.3.2. Stepped combination ... 6.4. From Julian Day Number to Julian Date ... 6.5. From Julian Date to Julian Day Number ... 6.6. From Gregorian Date to Julian Day Number ... 6.7. From Julian Day Number to Gregorian Date ... 6.8. Algorithm with a Curved Line ... 7. General Algorithm for Calculating a Running Day Number from a Calendar Date ... 8. General Algorithm for Calculating a Calendar Date from a Running Day Number ... 9. Fast Estimate of the Number of Days Between Two Dates

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 or Julian Date (JD) is much used for this in astronomy. This page explains how you can translate a date from the Gregorian calendar to the Julian Date, or the other way around.

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.

About 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 between 1 and 12, all monthly day numbers between 1 and 31, and for all Julian Day Numbers (including negative ones).

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.

xx[x]xtrunc(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. From Gregorian Date to Julian Day Number

The algorithm to calculate a Julian Day Number J from a Gregorian date (calendar year j, calendar month m (1 ≤ m ≤ 12), calendar day d) is:

  1. If m < 3, then replace m by m + 12 and j by j − 1.
  2. (Eq. 1) c = ⌊j/100⌋
  3. (Eq. 2) x = j − 100*c = j mod 100
  4. (Eq. 3) J₀ = 1721119.5
  5. (Eq. 4) J₁ = ⌊146097*c/4⌋
  6. (Eq. 5) J₂ = ⌊36525*x/100⌋
  7. (Eq. 6) J₃ = ⌊(153*m − 457)/5⌋
  8. (Eq. 7) J = J₁ + J₂ + J₃ + d − 1 + J₀ = ⌊146097*c/4⌋ + ⌊36525*x/100⌋ + ⌊(153*m − 457)/5⌋ + d − 1 + 1721119.5

Here J is the number of days from the beginning of calculation year 0 until the beginning of the current 100-calculation-year-period, J the number of days since the beginning of the current 100-calculation-year-period, and J the number of days minus one from the beginning of the current calculation year until the beginning of the current month.

For example: what is the Julian Day Number for 7 September 2010 on the Gregorian calendar? Then j = 2010, m = 9, d = 7 so c = 20, x = 10 and then J₁ = ⌊146097*20⁄4⌋ = 730485, J₁ = ⌊36525*10⁄100⌋ = 3652, J₂ = ⌊(153*9 − 457)/5⌋ = 184, en J = 730485 + 3652 + 184 + 7 − 1 + 1721119.5 = 2455446.5.

And here are the calculations for a few more dates:

calendar date jmdcxJJJJ
2000-02-29 2000 → 1999 2 → 14 29 19 99 693960 36159 337 2451603.5
2000-03-01 2000 3 1 20 0 730485 0 0 2451604.5
2001-02-28 2001 → 2000 2 → 14 28 20 0 730485 0 337 2451968.5
2001-03-01 2001 3 1 20 1 730485 365 0 2451969.5
2100-02-28 2100 → 2099 2 → 14 28 20 99 730485 36159 337 2488127.5
2100-03-01 2100 3 1 21 0 767009 0 0 2488128.5

3. From Julian Day Number to Gregorian Date

The algorithm to calculate a Gregorian date (calendar year j, calendar month m (1 ≤ m ≤ 12), calendar day d) from a Julian Day Number J is:

  1. (Eq. 8) x₂ = J − 1721119.5
  2. (Eq. 9) c₂ = ⌊(4*x₂ + 3)/146097⌋
  3. (Eq. 10) x₁ = x₂ − ⌊146097*c₂/4⌋
  4. (Eq. 11) c₁ = ⌊(100*x₁ + 99)/36525⌋
  5. (Eq. 12) x₀ = x₁ − ⌊36525*c₁/100⌋
  6. (Eq. 13) j = 100*c₂ + c
  7. (Eq. 14) m = ⌊(5*x₀ + 461)/153⌋
  8. (Eq. 15) d = x₀ − ⌊(153*m − 457)/5⌋ + 1
  9. If m > 12, then replace m by m − 12 and j by j + 1.

Here x is the number of days since the beginning of calculation year 0, c the number of calculation centuries since that time, x the number of days since the beginning of the current calculation century, c the number of calculation years since that time, and x the number of days since the beginning of the current calculation year.

For example, what is the Gregorian calendar date that corresponds to Julian Day Number 2455446.5? Then J = 2455446,5 so x₂ = 2455446.5 − 1721119.5 = 734327, c₂ = ⌊(4*734327 + 3)/146097⌋ = ⌊2937311⁄146097⌋ = 20, x₁ = 734327 − ⌊146097*20⁄4⌋ = 734327 − ⌊2921940⁄4⌋ = 734327 − 730485 = 3842, c₁ = ⌊(100*3842 + 99)/36525⌋ = ⌊384299⁄36525⌋ = 10, x₀ = 3842 − ⌊36525*10⁄100⌋ = 3842 − ⌊365250⁄100⌋ = 3842 − 3652 = 190, j = 100*20 + 10 = 2010, m = ⌊(5*190 + 461)/153⌋ = ⌊1411⁄153⌋ = 9, d = 190 − ⌊(153*9 − 457)/5⌋ + 1 = 190 − ⌊920⁄5⌋ = 190 − 184 + 1 = 7 so then j = 2010, m = 9, d = 7, so the date is 7 September 2010.

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

Jxcxcxjmdcalendar date
2451603.5 730484 19 36524 99 365 1999 14 29 2000-02-29
2451604.5 730485 20 0 0 0 2000 3 1 2000-03-01
2451968.5 730849 20 364 0 364 2000 14 28 2001-02-28
2451969.5 730850 20 365 1 0 2001 3 1 2001-03-01
2488127.5 767008 20 36523 99 364 2099 14 28 2100-02-28
2488128.5 767009 21 0 0 0 2100 3 1 2100-03-01

4. From Julian Date to Julian Day Number

The algorithm to calculate a Julian Day Number J from a Julian date (calendar year j, calendar month m (1 ≤ m ≤ 12), calendar day d) is:

  1. If m < 3, then replace m by m + 12 and j by j − 1.
  2. (Eq. 16) J₀ = 1721117.5
  3. (Eq. 17) J₁ = ⌊1461*j/4⌋
  4. (Eq. 18) J₂ = ⌊(153*m − 457)/5⌋
  5. (Eq. 19) J = J₁ + J₂ + d − 1 + J₀ = ⌊1461*j/4⌋ + ⌊(153*m − 457)/5⌋ + d − 1 + 1721117.5

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

For example: what is the Julian Day Number for 7 September 2010 on the Julian calendar? Then j = 2010, m = 9, d = 7 so J₁ = ⌊1461*2010⁄4⌋ = 734152, J₂ = ⌊(153*9 − 457)/5⌋ = 184, J = 734152 + 184 + 7 − 1 + 1721117.5 = 2455459.5.

And here are the calculations for a few more dates:

calendar date jmdJJJ
2000-02-29 2000 → 1999 2 → 14 29 730134 337 2451616.5
2000-03-01 2000 3 1 730500 0 2451617.5
2001-02-28 2001 → 2000 2 → 14 28 730500 337 2451981.5
2001-03-01 2001 3 1 730865 0 2451982.5
2100-02-28 2100 → 2099 2 → 14 28 766659 337 2488141.5
2100-03-01 2100 3 1 767025 0 2488142.5

5. From Julian Day Number to Julian Date

The algorithm to calculate a Julian date (calendar year j, calendar month m (1 ≤ m ≤ 12), calendar day d) from a Julian Day Number J is:

  1. (Eq. 20) x₁ = J − 1721117.5
  2. (Eq. 21) j = ⌊(4*x₁ + 3)/1461⌋
  3. (Eq. 22) x₀ = x₁ − ⌊1461*j/4⌋
  4. (Eq. 23) m = ⌊(5*x₀ + 461)/153⌋
  5. (Eq. 24) d = x₀ − ⌊(153*m − 457)/5⌋ + 1
  6. If m > 12, then replace m by m − 12 and j by j + 1.

Here x is the number of days since the beginning of calculation year 0, and x is the number of days since the beginning of the current calculation year.

For example, what is the Julian calendar date that corresponds to Julian Day Number 2455459.5? Then J = 2455459.5 so: x₁ = 2455459.5 − 1721117.5 = 734342, j = ⌊(4*734342 + 3)/1461⌋ = 2010, x₀ = 734342 − ⌊1461*2010⁄4⌋ = 190, m = ⌊(5*190 + 461)/153⌋ = ⌊1411⁄153⌋ = 9, d = 190 − ⌊(153*9 − 457)/5⌋ + 1 = 190 − ⌊920⁄5⌋ + 1 = 190 − 184 + 1 = 7 so then j = 2010, m = 9, d = 7, so the date is 7 September 2010.

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

Jxjxmdcalendar date
2451616.5 730499 1999 365 14 29 2000-02-29
2451617.5 730500 2000 0 3 1 2000-03-01
2451981.5 730864 2000 364 14 28 2001-02-28
2451982.5 730865 2001 0 3 1 2001-03-01
2488141.5 767024 2099 365 14 29 2100-02-28
2488142.5 767025 2100 0 3 1 2100-03-01

6. Derivation of the Algorithms

We need two methods to be able to derive the preceding algorithms:

  1. decomposition into periods
  2. approximate with a straight line

6.1. Decomposition into Periods

If the calendar consists of periods that keep repeating, where each longer period contains shorter periods that themselves keep repeating, and where only the very last shorter period within such a longer period may be even shorter than the preceding periods which are all equally long, then we can decompose the number of days since the common beginning of all of those periods into those periods.

In more mathematical language, let x be the number of days since the beginning, and let

(Eq. 25) x = p₁*c₁ + p₂*c₂ + … + pn*cn + x

Given that you know x and all pi (the periods), how do you calculate ci and x? We assume that all pi are greater than 0. If a particular pi is negative, then remove its minus sign. We sort the pi so that p₁ < p₂ < p₃ < … < pn. Then set

(Eq. 26) xn = x

and then for every i from n through 1:

(Eq. 27) ci = ⌊xi/pi

(Eq. 28) x(i−1) = xici*pi = xi mod pi

Let's apply this to the day number n in the current year.

All months in the Julian and Gregorian calendars have the same length in each year, except for February: that month has 29 days in a leap year, instead of 28 days. It is easier to calculate if the deviating thing is at the very end of the period, so we work with periods of a year that begin with March 1st and that run until just before the next March 1st. Let's call that the calculation year. If there is a leap day, then it comes at the very end of the calculation year. For convenience we keep calling March month number 3 (of the calculation year), so then January becomes month number 13 and February becomes month number 14. We regard January and February of a particular calendar year as the last two months of the preceding calculation year.

The lengths of the months in the calculation year then are:

length 31 30 31 30 31 31 30 31 30 31 31 28 of 29
month Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb
number 3 4 5 6 7 8 9 10 11 12 13 14

We see two successive sets of 31-30-31-30-31 days (March through July, and then August through December) and then part of a third such set (January and February), but that one stops after 28 or 29 days in the second month of the set. Such a period of 31-30-31-30-31 is 153 days long, so we can say that the year is divided into periods of 153 days, and each such a period has the same sequence of month lengths, until the year is full.

Within such a period of 153 days we see three repeats of the sequence 31-30 days (61 days), again with the last repeat truncated (because that one only contains one month). And within such a period of 61 days we see a repeat of periods (months) of 31 days, again with the last period truncated, because that one contains only 30 days.

All in all we can say that the day number n since the beginning of the calculation year (March 1 has n = 1), and the month number m in the calculation year (m = 3 through 12 for March through December, and 13 and 14 for the immediately following January and February) are equal to

(Eq. 29) n − 1 = Q₁ = 153*c₃ + 61*c₂ + 31*c₁ + d − 1

(Eq. 30) m − 3 = Q₂ = 5*c₃ + 2*c₂ + c

where all c numbers are whole numbers greater than or equal to 0, and c is less than 2, c is less than 5, and c is less than 3.

For September 7th (d = 7, m = 9) and equation 30 we find x = 6 = m − 3 and p₃ = 5, p₂ = 2, p₁ = 1. Then

x₃ = x = 6

c₃ = ⌊x₃/p₃⌋ = ⌊6⁄5⌋ = 1

x₂ = x₃ − cp₃ = 6 − 1*5 = 1 = 6 mod 5 = x₃ mod p

c₂ = ⌊x₂/p₂⌋ = ⌊1⁄2⌋ = 0

x₁ = x₂ − cp₂ = 1 − 0*2 = 1

c₁ = ⌊x₁/p₁⌋ = ⌊1⁄1⌋ = 1

x₀ = x₁ − cp₁ = 0

So c₃ = 1, c₂ = 0, c₁ = 1. Entered into equation 29, this leads to

n − 1 = 153*1 + 61*0 + 31*1 + 7 − 1 = 190

so n = 191. Now we go in the opposite direction.

Now we go in the opposite direction. Then x = n − 1 = 190, p₃ = 153, p₂ = 61, p₁ = 31, so

x₃ = x = 190

c₃ = ⌊x₃/p₃⌋ = ⌊190⁄153⌋ = 1

x₂ = x₃ − cp₃ = 190 − 1*153 = 37

c₂ = ⌊x₂/p₂⌋ = ⌊37⁄61⌋ = 0

x₁ = x₂ − cp₂ = 37 − 0*61 = 37

c₁ = ⌊x₁/p₁⌋ = ⌊37⁄31⌋ = 1

x₀ = x₁ − cp₁ = 37 − 1*31 = 6

so c₃ = 1, c₂ = 0, c₁ = 1 like we found before, and hence m − 3 = 5*1 + 2*0 + 1 = 6 so m = 9, and also d − 1 = x so d = 7.

Warning! Don't remove the − 1 from the left hand side and right hand side of equation 29, otherwise the cs aren't correct anymore in some cases, such as for n = 31. Equation 29 then yields n − 1 = 30 = Q₁ = 153*0 + 61*0 + 31*0 + d − 1 so d = 31 and (from equation 30) m − 3 = 5*0 + 2*0 + 0 = 0 so m = 3, which means March 31st. If the − 1 are removed then equation 29 yields

(Eq. 31) n = 153*c₃ + 61*c₂ + 31*c₁ + d (wrong!)

which in this case yields 31 = 153*0 + 61*0 + 31*1 + 0 and then equation 30 leads to m − 3 = 5*0 + 2*0 + 1 = 1 so m = 4, which means April 0th ― which is incorrect.

6.2. Algorithm with the Straight Line

Another method is to find a straight line that (with rounding) yields the right results. If there are only two different month lengths that differ by only one day (except that the very last month may be shorter), then you can try to find such a line.

First we prepare some formulas. Let

(Eq. 32) y = y₀ + z

(Eq. 33) y₀ = ⌊px

for whole numbers x, y, z, with p ≥ 1, p not infinitely large, and 0 ≤ z ≤ ⌈p. x is the number of long periods (let's call them "months"), y the continuous number of short periods ("days"), and z the number of short periods since the beginning of the current long period ("days in this month"). Then when month number x increases by one, then y increases by either p or p.

In the other direction we have

(Eq. 34) x = ⌈(y + 1)/p⌉ − 1

(Eq. 35) z = y − ⌊px

where one or more values of day number y yield the same month number x. With these formulas, y = 0 corresponds to x = 0, z = 0.

That these pairs of formulas are each other's inverse can be seen as follows. First we rewrite equation 33 to

(Eq. 36) y₀ = ⌊px⌋ = px − δ

(Eq. 37) 0 ≤ δ < 1

where δ is the difference that must be subtracted from px to find the nearest smaller whole number. Formulas 32 and 37 then lead to

(Eq. 38) y = ⌊px⌋ + z = px − δ + z

(Eq. 39) −1 < −δ ≤ 0

(Eq. 40) px + z < y + 1 = px + 1 − δ + zpx + z + 1

(Eq. 41) x + z/p < (y + 1)/px + (z + 1)/p

If now z = 0 then we get from equation 41

(Eq. 42) x < (y + 1)/px + 1/p

Rounding up cannot decrease a value, so it follows that

(Eq. 43) x < ⌈(y + 1)/p⌉ ≤ ⌈x + 1/p

Omdat x een heel getal is is x⌉ = x. Omdat p ≥ 1 is maar p wel eindig is is ook 0 < 1/p ≤ 1, dus is x + 1/p⌉ = x + 1. Hiermee volgt uit vergelijking 43

Because x is a whole number, we have x⌉ = x. Because p ≥ 1 and p is finite, we have 0 < 1/p ≤ 1, so x + 1/p⌉ = x + 1. With these, equation 43 yields

(Eq. 44) x < ⌈(y + 1)/p⌉ ≤ x + 1

(Eq. 45) x − 1 < ⌈(y + 1)/p⌉ − 1 ≤ x

(Eq. 46) x − 1 < x*x

The only whole number x* that matches these conditions is x* = x. So if z = 0 then you can use equation 34 to calculate the x that goes with the given y.

Now we look at the case where z = −1. That is the day before z = 0, but z = 0 belonged to the first day of the month, so z = −1 belongs to the previous month, i.e., to an x that is one less. With z = −1, equation 41 becomes

(Eq. 47) x − 1/p < (y + 1)/px

Because 1/p ≤ 1, we also have x − 1 ≤ x − 1/p so then

(Eq. 48) x − 1 < (y + 1)/px

(Eq. 49) x − 1 < ⌈(y + 1)/p⌉ ≤ x

(Eq. 50) x − 2 < ⌈(y + 1)/p⌉ − 1 ≤ x − 1

(Eq. 51) x − 2 < x*x − 1

The only whole number x* that matches these conditions is x* = x − 1. So we find that indeed z = −1 ends up in the month one earlier than for z = 0. Equation 34 puts the shift from month x − 1 to month x just before z = 0, where it belongs.

Here are some examples of x, y, z for p > 1. The switch from x = −1 to x = 0 occurs just before day y = 0.

yxyz
−1 −1 ⌊−p−1 − ⌊−p
0 0 0 0
1 0 0 1
p⌋ − 1 0 0 p⌋ − 1
p 1 p 0

It is inconvenient that two different kinds of rounding are used in these formulas. Rounding up (⌈⌉) is not often available on electronic calculators. We can transform rounding up into rounding down using (for any number s)

(Eq. 52) s⌉ = −⌊−s

If r can be written as a ratio r = m/n with m, n whole numbers, then also

(Eq. 53) r⌉ = ⌈m/n⌉ = ⌊(m + n − 1)/n

So let's define

(Eq. 54) p = f/g

with f, g whole numbers. Then g "months" correspond to f "days". Our demand that p ≥ 1 corresponds to fg. Then equation 34 yields

(Eq. 55) x = ⌈(y + 1)/p⌉ − 1 = ⌈(y + 1)*g/f⌉ − 1 = ⌈(gy + gf)/f⌉ = ⌊(gy + g − 1)/f

and equations 32ff

(Eq. 56) y = y₀ + z

(Eq. 57) y₀ = ⌊fx/g

(Eq. 58) z = y − ⌊fx/g

If x increases by 1, then y increases by p or p. What can we say about when it is p and when it is p? Let

(Eq. 59) q = [p]

(Eq. 60) p = q + ψ

Then −½ < ψ ≤ +½ and q is the most frequent increase of y (when x keeps increasing in steps of 1). Let's call the other increase of y (the less popular one) the leap value. The leap value is p⌋ = q − 1 if ψ > 0, or p⌉ = q + 1 if ψ < 0. When we insert equation 60 into equation 32 then we find

(Eq. 61) y = ⌊px⌋ = ⌊(q + ψ)x⌋ = qx + ⌊ψx

because q and x are whole numbers. If x increases in steps of 1, then y increases in steps of q, until ⌊ψx changes by 1, and then y increases in a step equal to the leap value. ψ fits 1/|ψ| times into 1, so x must increase by ⌊1/|ψ|⌋ or ⌈1/|ψ|⌉ before y increases by the leap value again.

Let

(Eq. 62) Q = 1/|ψ| = 1/(p − [p])

Then the increase in x between two successive leap values is sometimes equal to Q and sometimes equal to Q.

Here is an example with p = f/g = 4⁄3. First we go from x to y, using equation 32.

xy
0 ⌊(4/3)*0⌋ = ⌊0⌋ = 0
1 ⌊(4/3)*1⌋ = ⌊4/3⌋ = 1
2 ⌊(4/3)*2⌋ = ⌊8/3⌋ = 2
3 ⌊(4/3)*3⌋ = ⌊12/3⌋ = 4
4 ⌊(4/3)*4⌋ = ⌊16/3⌋ = 5
5 ⌊(4/3)*5⌋ = ⌊20/3⌋ = 6
6 ⌊(4/3)*6⌋ = ⌊24/3⌋ = 8

So y increases with 1, 1, and 2 (i.e., in 3 = g periods it increases by a total of 4 = f, and then increases repeat themselves again. Here a regular period lasts 1, and a leap period lasts 2.

Equation 62 yields Q = 3, which is correct, because we saw above that the leap value returns every 3 periods.

Now we go from y to x. xa stands for x from equation 34, xb for x from equation 55. Equation 55 then becomes x = ⌊(3*y + 2)/4⌋.

yxaxb
0 ⌈0.75⌉ − 1 = 0 ⌊0.5⌋ = 0
1 ⌈1.5⌉ − 1 = 1 ⌊1.25⌋ = 1
2 ⌈2.25⌉ − 1 = 2 ⌊2⌋ = 2
3 ⌈3⌉ − 1 = 2 ⌊2.75⌋ = 2
4 ⌈3.75⌉ − 1 = 3 ⌊3.5⌋ = 3

With formulas 32 and 56 we find month lengths equal to p or p in a fixed sequence. The month with x = 0 always has p days, and the two successive months with p days or the two successive months with p days always belong (for a particular p) to a particular x.

For f = 153, g = 5 (hence p = 153⁄5 = 30.6) we find

(Eq. 63) y = ⌊fx/g⌋ + z = ⌊153*x/5⌋ + z = y₀ + z

(Eq. 64) x = ⌊(gy + g − 1)/f⌋ = ⌊(5*y + 4)/153⌋

with corresponding month lenths l:

x 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
y 0 30 61 91 122 153 183 214 244 275 306 336 367 397 428 459
l30 31 30 31 31 30 31 30 31 31 30 31 30 31 31

The sequence of month lengths that we seek from March (the beginning of the calculation year) is in this sequence, but begins at x = 4 and y₀ = 122. For translating a day number to a date it is important that the month number and day number begin at 0, so we shift them:

(Eq. 65) x = xa + a

(Eq. 66) y = ya + ⌊pa⌋ = ya + ⌊fa/g

Then we find

(Eq. 67) y = ⌊fx/g⌋ + zya + ⌊fa/g⌋ = ⌊f*(xa + a)/g⌋ + z

(Eq. 68) x = ⌊(gy + g − 1)/f⌋ ⇔ xa + a = ⌊(g*(ya + ⌊fa/g⌋) + g − 1)/f

hence

(Eq. 69) ya = ⌊f*(xa + a)/g⌋ − ⌊fa/g⌋ + z = ya0 + z

(Eq. 70) xa = ⌊(gya + gfa/g⌋ + gfa − 1)/f

For the Julian and Gregorian calendars we find a = 4 and pa⌋ = ⌊4*153⁄5⌋ = 122. From that we find

(Eq. 71) ya = ⌊153*(xa + 4)/5⌋ − 122 + z = ⌊(153*xa + 2)/5⌋ + z = ya0 + z

(Eq. 72) xa = ⌊(5ya + 2)/153⌋

We now want to give the first month of the calculation year the label m = 3 rather than xa = 0. We achieve that by defining

(Eq. 73) m = xa + 3

and then we find

(Eq. 74) ya = ⌊(153*(m − 3) + 2)/5⌋ + z = ⌊(153*m − 457)/5⌋ + z

(Eq. 75) m = ⌊(5*ya + 2)/153⌋ + 3 = ⌊(5*ya + 3*153 + 2)/153⌋ = ⌊(5*ya + 461)/153⌋

Here are some calculations for three days around the beginning of the calculation year.

yaxamya0zdate
−1 −1 2 −31 30 last day of the preceding calculation year
0 0 3 0 0 1 March
1 0 3 0 1 2 March

And here are the month lengths for xa, ya, m.

m 3 4 5 6 7 8 9 10 11 12 13 14 15
xa 0 1 2 3 4 5 6 7 8 9 10 11 12
ya0 0 31 61 92 122 153 184 214 245 275 306 337 367
l 31 30 31 30 31 31 30 31 30 31 31 30

6.3. 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:

(Eq. 76) y₁ = ⌊fx₁/g₁⌋ + z

(Eq. 77) y₂ = ⌊fx₂/g₂⌋ + z

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 to z or to x. 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) is stepped, i.e. of the second kind. 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. 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). The length of the year depends on the length of the months, because the year is 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.

6.3.1. Flat combination

In this case z₂ = y, so

(Eq. 78) y₂ = ⌊fx₂/g₂⌋ + ⌊fx₁/g₁⌋ + z

and in the other direction

(Eq. 79) x₂ = ⌊(gy₂ + g₂ − 1)/f₂⌋

(Eq. 80) z₂ = y − ⌊fx₂/g₂⌋

(Eq. 81) x₁ = ⌊(gz₂ + g₁ − 1)/f₁⌋

(Eq. 82) z₁ = y − ⌊fx₁/g₁⌋

Here (for example) y is the running day number, x is the year number, x is the month number in the current year (with 0 for the first month), and z is the day number in the current month (with 0 for the first day).

6.3.2. Stepped combination

In this case x₂ = y, so

(Eq. 83) y₂ = ⌊f₂(⌊fx₁/g₁⌋ + z₁)/g₂⌋ + z

and in the other direction

(Eq. 84) x₂ = ⌊(gy₂ + g₂ − 1)/f₂⌋

(Eq. 85) z₂ = y − ⌊fx₂/g₂⌋

(Eq. 86) x₁ = ⌊(gx₂ + g₁ − 1)/f₁⌋

(Eq. 87) z₁ = y − ⌊fx₁/g₁⌋

Here (for example) y is the running day number, x is the year number, z is the month number in the current year (with 0 for the first month), and z is the day number in the current month (with 0 for the first day).

6.4. From Julian Day Number to Julian Date

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.

We get good results with f = 1461, g = 4; then the first three out of every four years have 365 days and the fourth one is a leap year with 366 days. The first day of calculation year 0, i.e. 1 March of the year 0 in the Julian calendar, has Julian Day Number J₀ = 1721117.5.

We use formulas 69 and 70 with ya = x₁ = JJ, xa = j (the calculation year number), a = 0. Then z = x is the day number in the calculation year (z = 0 for the first day, March 1st). Then

(Eq. 88) j = ⌊(4*x₁ + 3)/1461⌋

(Eq. 89) x₀ = x₁ − ⌊1461*j/4⌋

Now we must translate that x into a month number m and a day number d. We use formulas 74 and 75 (which is the same as using formulas 69 and 70, but now with f = 153, g = 5, and a = 4), with ya = x from the previous calculation. We find

(Eq. 90) m = ⌊(5*x₀ + 461)/153⌋

(Eq. 91) z = x₀ − ⌊(153*m − 457)/5⌋

This z counts days from z = 0 for the first day of the month. To get the day number d with d = 1 for the first day of the month, we set

(Eq. 92) d = z + 1

6.5. From Julian Date to Julian Day Number

We begin with the smallest period. We calculate the day-number-within-the-year from equations 92 and 91

(Eq. 93) x₀ = ⌊(153*m − 457)/5⌋ + d − 1

This x now becomes the z in the second calculation. We use equation 69. Like before we have ya = JJ, so

(Eq. 94) JJ₀ = ⌊1461*j/4⌋ + z = ⌊1461*j/4⌋ + ⌊(153*m − 457)/5⌋ + d − 1

6.6. From Gregorian Date to Julian Day Number

To approximate Gregorian years with a straight line, the leap values have to return after every Q or Q periods (for a well-chosen Q; see equation 60ff). 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:

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₀ = 1721119.5.

We begin with the smallest period. We calculate the day-number-within-the-year from equations 92 and 91

(Eq. 95) x₀ = ⌊(153*m − 457)/5⌋ + d − 1

This x now becomes the z in the second calculation. We use equation 69, with f = 36525, g = 100, a = 0 to calculate the day number within the current calculation century.

(Eq. 96) c = ⌊j/100⌋

(Eq. 97) x = j − 100*c

(Eq. 98) x₁ = ⌊36525*x/100⌋ + x

And this x in turn becomes the z in the third calculation, to include the 400-year periods, with f = 146097, g = 4, a = 0.

(Eq. 99) x₂ = ⌊146097*c/4⌋ + x

And this x is equal to JJ. All in all we find

(Eq. 100) JJ₀ = ⌊146097*c/4⌋ + ⌊36525*x/100⌋ + ⌊(153*m − 457)/5⌋ + d − 1

6.7. From Julian Day Number to Gregorian Date

We begin with equations 69, 70 for the 146097/4 line. Then f = 146097, g = 4, ya = x₂ = J₁ − J, xa = c (the number of calculation centuries), and a = 0. z = x is then the day number within the current calculation century, to which we'll fit the 36525/100 line later.

(Eq. 101) c₂ = ⌊(4*x₂ + 3)/146097⌋

(Eq. 102) x₁ = x₂ − ⌊146097*c₂/4⌋

Now we use equations 69, 70 again, but now for the 36525/100 line, with f = 36525, g = 100, ya = x, xa = c (the number of calculation years within the current calculation century), and a = 0. z = x is then the day number within the current calculation year, to which we'll fit the 153/5 line later.

(Eq. 103) c₁ = ⌊(100*x₁ + 99)/36525⌋

(Eq. 104) x₀ = x₁ − ⌊36525*c₁/100⌋

And now we use equations 74, 75 for the last line. That works the same here as it did for the Julian Calendar.

(Eq. 105) m = ⌊(5*x₀ + 461)/153⌋

(Eq. 106) d = x₀ − ⌊(153*m − 457)/5⌋ + 1

(Eq. 107) j = 100*c₂ + c

6.8. 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

(Eq. 108) v'(x) ≥ 1

(Eq. 109) v(w(x)) = w(v(x)) = x

(so v and w are each other's inverse functions) then the calendar formulas become

(Eq. 110) y = ⌊v(x)⌋ + z

(Eq. 111) x = ⌈(w(y + 1)⌉ − 1 = −⌊−w(y + 1)⌋ − 1

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).

7. General Algorithm for Calculating a Running Day Number from a Calendar Date

Here is an algorithm (in pseudocode) for calculating a running day number from a calendar date, for a calendar with only flat combinations of periods.

q[i,j] is an array of calendar coefficients with 3 elements in the first dimension that describe how to handle one calendar period. The second dimension selects different calendar periods, from large to small. n is the number of elements in the second dimension (i.e., the number of different calendar periods).

c[i,j] is the array of calendar dates, in which the first dimension selects the different calendar periods and the second dimension selects the different calendar dates. There are n + 1 elements in the first dimension, and t different elements in the second dimension (i.e., t different calendar dates).

d[i] is the array of running day numbers. It has t elements.

The function DIV(x,y) corresponds to x/y.

Here is the algorithm:

function CAL2DAY(c, q)
  for j from 0 through t−1:
    set d[j] to 0.
  for i from 0 through n−1:
    set s[i] to DIV(q[0,i]*q[2,i],q[1,i])
  for i from 0 through n−1:
    for j from 0 through t−1:
      set d[j] to d[j] + DIV(q[0,i]*(c[i,j] + q[2,i]),q[1,i]) − s[j]
  for j from 0 through t−1:
    set d[j] to d[j] + c[n,j]
  return d
end function

The c[i,j] all begin at 0 for the first day.

If you want to use this function for the Julian calendar then q should contain the following elements: [ [ 1461, 4, 0 ], [ 153, 5, 4 ] ]. c[0,j] is then the calculation year number, c[1,j] is the month number within the calculation year (March is month number 0!), and c[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 c[0,j] is the calculation century number, c[1,j] the calculation year within the calculation century, c[2,j] the month number within the calculation year (March has month number 0!), and c[3,j] the day number within the month (the first day has number 0!).

Here is pseudocode to calculate a Julian Day Number from a Julian calendar date c:

function JUL2JD(c)
  set q to [ [ 1461, 4, 0 ], [ 153, 5, 4 ] ]
  for i from 0 through n−1:
    set c[1,i] to c[1,i] − 3
    set c[2,i] to c[2,i] − 1
    if c[1,i] is less than 0:
      set c[1,i] to c[1,i] + 12
      set c[0,i] to c[0,i] − 1
  set d to CAL2DAY(c,q)
  for j from 0 through t−1:
    set d[j] to d[j] + 1721117,5
  return d
end function

Here is pseudocode to calculate a Julian Day Number from a Gregorian calendar date c:

function GREG2JD(c)
  set q to [ [ 146097, 4, 0 ], [ 36525, 100, 0 ], [ 153, 5, 4 ] ]
  for i from 0 through n−1:
    set c[1,i] to c[1,i] − 3
    if c[1,i] is less than 0:
      set c[1,i] to c[1,i] + 12
      set c[0,i] to c[0,i] − 1
    set b[0,i] to DIV[c[0,i],100]
    set b[1,i] to c[0,i] − 100*b[0,i]
    set b[2,i] to c[1,i]
    set b[3,i] to c[2,i] − 1
  set d to CAL2DAY(b,q)
  for j from 0 through t−1:
    set d[j] to d[j] + 1721119,5
  return d
end function

8. General Algorithm for Calculating a Calendar Date from a Running Day Number

Here is a general algorithm (in pseudocode) for calculating a calendar date from a running day number, in similar fashion as above.

function DAY2CAL(d, q)
  for i from 0 through n−1:
    set s[i] to DIV(q[0,i]*q[2,i],q[1,i])
    set r[i] to q[1,i]*s[i] + q[1,i] − q[0,i]*q[2,i] − 1
  for i from 0 through n−1:
    for j from 0 through t−1:
      set c[i,j] to DIV(q[1,i]*d[j] + r[i],q[0,i])
      set d[j] to d[j] − DIV(q[0,i]*(c[i,j] + q[2,i]),q[1,i]) + s[j]
  for j from 0 through t−1:
    set c[n,j] to d[j]
  return c
end function

For the same calendar, you should use the same q here as for CAL2DAY above.

Here is pseudocode to calculate Julian calendar dates from Julian day numbers d:

function JD2JUL(d)
  set q to [ [ 1461, 4, 0 ], [ 153, 5, 4 ] ]
  for j from 0 through t−1:
    set d[j] to d[j] − 1721117,5
  set c to DAY2CAL(d,q)
  for i from 0 through n−1:
    set c[1,i] to c[1,i] + 3
    set c[2,i] to c[2,i] + 1
    if c[1,i] is greater than 12:
      set c[1,i] to c[1,i] − 12
      set c[0,i] to c[0,i] + 1
  return c
end function

And here is pseudocode to calculate Gregorian calendar dates from Julian day numbers d:

function JD2GREG(d)
  set q to [ [ 146097, 4, 0 ], [ 36525, 100, 0 ], [ 153, 5, 4 ] ]
  for j from 0 through t−1:
    set d[j] to d[j] − 1721119,5
  set b to DAY2CAL(d,q)
  for i from 0 through n−1:
    set c[0,i] to 100*b[0,i] + b[1,i]
    set c[1,i] to b[2,i] + 3
    set c[2,i] to b[3,i] + 1
    if c[1,i] is greater than 12:
      set c[1,i] to c[1,i] − 12
      set c[0,i] to c[0,i] + 1
  return c
end function

9. 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 Julian Day numbers 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 II 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

(Eq. 112) n = ⌊a j + b m + c d

with a, b, and c from the following table:

abc|Δ|maxσp0
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 |Δ|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 sigma shows the standard deviation (say, the average error, also measured in days). The p0 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.



[AA]

languages: [en] [nl]

http://www.astro.uu.nl/~strous/AA/en/reken/juliaansedag.html;
Last updated: 2010-01-27