civil_jdn

Description

civil_jdn converts a provided civil (Julian or Gregorian) date to the Julian Day Number (i.e. the number of days since January 1 in the year 4713 BC).

Notes

  1. This routine works for both the Gregorian calendar and the Julian calendar.
  2. The year zero never existed. the year 1 BC was immediately followed by the year 1 AD. When calling this function, you should be ware that the value 0 for year, means 1 BC, -1 means 2 BC et cetera.

Gory Details

The formula used to calculate the julian day number depends on the date. A conditional expression tests whether the date is before or after October 15, 1582, which is when the Gregorian calendar was implemented.

The Gregorian calendar was proposed by Aloysius Lilius, a physician from Naples, to correct for errors in the older Julian Calendar. It was decreed by Pope Gregory XIII in a papal bull on 24 February 1582. The papal bull decreed that 10 days should be dropped from October 1582 so that 15 October should follow immediately after 4 October, and from then on the reformed calendar should be used.

This was observed in Italy, Poland, Portugal, and Spain. Other Catholic countries followed shortly after, but Protestant countries were reluctant to change, and the Greek orthodox countries didn't change until the start of the 1900s.

The algorithm presented here, uses the date of October 15, 1582 as the starting point for the Gregorian Calendar. It should not be hard to modify the code, to make it use the the date for change in your country.

At the beginning of the function, all argument are converted to long integers, using the CLng function (see also Notes.) This is not necessary in all programming languages, but Visual Basic can run into an overflow when multiplying an integer with a large number, even if the result of the entire formula is to be stored in a long integer variable.

The optional parameter CalendarType can be used to force the routine to interpret the civil date as a julian date. Setting this parameter to 2 (or any other value different from 1) makes civil_jdn call julian_jdn, which can be very usefull in other routines.

Code section

Const Gregorian = 1
Const Julian = 2

Function civil_jdn(ByVal iYear As Integer, _
                   ByVal iMonth As Integer, _
                   ByVal iDay As Integer, _
                   Optional ByVal calendarType As Integer = Gregorian) As Long
    Dim lYear As Long
    Dim lMonth As Long
    Dim lDay As Long

    If calendarType = Gregorian And ((iYear > 1582) Or _
        ((iYear = 1582) And (iMonth > 10)) Or _
        ((iYear = 1582) And (iMonth = 10) And (iDay > 14))) _
    Then
        lYear = CLng(iYear)
        lMonth = CLng(iMonth)
        lDay = CLng(iDay)
        civil_jdn = ((1461 * (lYear + 4800 + ((lMonth - 14) \ 12))) \ 4) _
            + ((367 * (lMonth - 2 - 12 * (((lMonth - 14) \ 12)))) \ 12) _
            - ((3 * (((lYear + 4900 + ((lMonth - 14) \ 12)) \ 100))) \ 4) _
            + lDay - 32075
    Else
        civil_jdn = julian_jdn(iYear, iMonth, iDay)
    End If

End Function

See also

jdn_civil, julian_jdn, Notes, Operators, Claus Tøndering's Calendar Faq.

Last update

Julian Day Number: 2452096
Civil (Gregorian) date: 5 July 2001
Julian date: 22 June 2001
Hebrew date: 14 Tammuz 5761
Islamic date: 13 Rabi' II 1422

Back to Calendar Math.

mail me
Kees Couprie

Other pages by the same author.