Post Reply 
(12C) Gregorian Date to Julian Day Number and vice versa
01-14-2019, 12:54 PM (This post was last modified: 01-14-2019 02:45 PM by Dieter.)
Post: #1
(12C) Gregorian Date to Julian Day Number and vice versa
The Julian Day number (JDN) of a date in the Gregorian calendar can be easily calculated with the 12C's built-in date functions. These also allow to convert a JDN back to a Gregorian date. Since that calendar started on 15 October 1582, this is the earliest possible date. The 12C will return an error for earlier dates.

The following program automatically chooses the appropriate conversion: if the input is an integer it cannot be a date so it has to be a JDN which is then converted into a date. If the input has a fractional part it is considered a date which is converted to the corresponding JDN.

The program works independent of the date format setting, both D.MY and M.DY are fine. This is achieved by selecting 1 Jan 2000 as a reference date which is entered/displayed as 1,012000 in either date mode. Its corresponding JDN is 2451545.

To convert a date to its JDN the program determines the number of days since 1 Jan 2000 (this may be positive or negative) and then adds 2451545 to the result.

To convert a JDN back to the corresponding date, 2451545 is subtracted (giving the number of days before/after 1 Jan 2000) and then the latter date is added.

Code:
01 FRAC
02 x=0?
03 GTO 20
04 1
05 ,
06 0
07 1
08 2
09 LstX
10 ΔDYS
11 2
12 4
13 5
14 1
15 5
16 4
17 5
18 +
19 GTO 00
20 LstX
21 2
22 4
23 5
24 1
25 5
26 4
27 5
28 -
29 1
30 ,
31 0
32 1
33 2
34 X<>Y
35 DATE
36 GTO 00

Example (assuming D.MY mode):

4,071979 [R/S] => 2444059
2444444  [R/S] => 23,071980

If the real 12C hehaves like HP's emulator you should even see "23.07.1980  3" for a moment, indicating the day of week (Wednesday).

Dieter
Find all posts by this user
Quote this message in a reply
01-14-2019, 03:50 PM (This post was last modified: 01-14-2019 11:28 PM by Albert Chan.)
Post: #2
RE: (12C) Gregorian Date to Julian Day Number and vice versa
(01-14-2019 12:54 PM)Dieter Wrote:  Example (assuming D.MY mode):

4,071979 [R/S] => 2444059
2444444  [R/S] => 23,071980

Perhaps calculation via Modified Julian Date Number is easier: https://www.hermetic.ch/cal_stud/jdn.htm

MJD = JD - 2,400,000.5 = (JD-0.5) - 24e5

Store date, Nov 16, 1858, to memory 0

Redo above examples:

RCL 0 4.071979 ΔDYS ==> 44059

RCL 0 44444 DATE ==> 23,07,1980 3

Edit: stored date were off by 1, to compensate for rounded-up JD
Officially, MJD(Nov 17, 1858, 00:00:00) = 0

17.111858 Enter 4.071979 ΔDYS
=> 44058 MJD
=> 2444058.5 JD, rounded-up to 2444059
Find all posts by this user
Quote this message in a reply
01-14-2019, 04:58 PM (This post was last modified: 01-14-2019 05:09 PM by Dieter.)
Post: #3
RE: (12C) Gregorian Date to Julian Day Number and vice versa
(01-14-2019 03:50 PM)Albert Chan Wrote:  Perhaps calculation via Modified Julian Date Number is easier:

It's not easier, as it's essentially the same calculation. And since 16,111858 ≠ 11,161858 it does not work for both date modes.
But there is one advantage: you can quickly do the calculation manually, once the base date has been stored somewhere, as shown in your examples.

BTW, please let's be precise here and distinguish between Julian dates (JD) and Julian day numbers (JDN).

(01-14-2019 03:50 PM)Albert Chan Wrote:  https://www.hermetic.ch/cal_stud/jdn.htm

Yes, there are lots of such variations, cf. https://en.wikipedia.org/wiki/Julian_day#Variants.
But the program is supposed to calculate the original JDN, not a modified variant. Since for the 12C the hardware limitations that led to most of such modifications do not apply I don't think that these make much sense.

Dieter
Find all posts by this user
Quote this message in a reply
01-14-2019, 05:30 PM (This post was last modified: 01-14-2019 06:37 PM by Albert Chan.)
Post: #4
RE: (12C) Gregorian Date to Julian Day Number and vice versa
(01-14-2019 04:58 PM)Dieter Wrote:  BTW, please let's be precise here and distinguish between Julian dates (JD) and Julian day numbers (JDN).

I was using the abbreviation from https://www.hermetic.ch/cal_stud/jdn.htm, section 6:

"Given a Julian day number JD, the modified Julian day number MJD is defined as MJD = JD - 2,400,000.5"

But, now that you mentioned it ... what is the difference ?
Find all posts by this user
Quote this message in a reply
01-14-2019, 05:52 PM (This post was last modified: 01-14-2019 06:31 PM by Dieter.)
Post: #5
RE: (12C) Gregorian Date to Julian Day Number and vice versa
(01-14-2019 05:30 PM)Albert Chan Wrote:  I was using the abbreviation from https://www.hermetic.ch/cal_stud/jdn.htm, section 6:

"Given a Julian day number JD, the modified Julian day number MJD is defined as MJD = JD - 2,400,000.5"

But, now that you mentioned it ... what is the difference ?

My understanding is that Julian dates may include the time as fractional part, i.e. JD 2444059 is 4 Jul 1979 at noon and 2444059,6 is 5 Jul 1979 at 2:24 h. One the other hand the Julian day number (sic!) is an integer indicating the ordinal number of that day (from 0:00 to 23:59'59,999...."), starting at a certain point in the past (here 1 Jan 4713 BC). This causes an offset of 0,5 between both definitions, apart from the one being a real number and the other an integer.

Please correct me if I'm wrong here.

Addendum: The German Wikipedia article on Julian Dates includes two good and concise commented algorithms for the conversion between dates in the Julian and Gregorian calendar and the Julian date / day number. There also is a detailled and comprehensive explanation how the formula works. So at least if you read German you should take a look at that.

Dieter
Find all posts by this user
Quote this message in a reply
01-14-2019, 06:27 PM (This post was last modified: 01-14-2019 06:50 PM by Albert Chan.)
Post: #6
RE: (12C) Gregorian Date to Julian Day Number and vice versa
(01-14-2019 05:52 PM)Dieter Wrote:  My understanding is that Julian dates may include the time as fractional part ...
Julian day number (sic!) is an integer indicating the ordinal number of that day ...

I thought so too, until I tried Julian Day Number Calculator.

For Jan 1, 2000, it calculated a Julian Day Number of 2451544.5 Big Grin
Find all posts by this user
Quote this message in a reply
01-14-2019, 09:28 PM
Post: #7
RE: (12C) Gregorian Date to Julian Day Number and vice versa
(01-14-2019 06:27 PM)Albert Chan Wrote:  I thought so too, until I tried Julian Day Number Calculator.

From the sources:
Code:
<!-- This is what an HTML comment looks like (This line terminates HTML comment) -->

Good to know. This gave me a chuckle.
And the fact that the page was was last modified on 980126.
Will we eventually find a Y2K bug?

The algorithm is described on a related page:
Quote:It is easy (with your calculator) to calculate the Julian Day Number of any date given on the Gregorian Calendar. The Julian Day Number so calculated will be for 0 hours, GMT, on that date. Here's how to do it:

1) Express the date as Y M D, where Y is the year, M is the month number (Jan = 1, Feb = 2, etc.), and D is the day in the month.

2) If the month is January or February, subtract 1 from the year to get a new Y, and add 12 to the month to get a new M. (Thus, we are thinking of January and February as being the 13th and 14th month of the previous year).

3) Dropping the fractional part of all results of all multiplications and divisions, let

A = Y/100
B = A/4
C = 2-A+B
E = 365.25x(Y+4716)
F = 30.6001x(M+1)
JD= C+D+E+F-1524.5

This is the Julian Day Number for the beginning of the date in question at 0 hours, Greenwich time. Note that this always gives you a half day extra. That is because the Julian Day begins at noon, Greenwich time. This is convenient for astronomers (who until recently only observed at night), but it is confusing.

But this appears to be what's calculated in the JavaScript sources:
Code:
    if(M<3)    {
        Y--;
        M += 12;
    }

//alert("D= " + D);
//alert("M= " + M);
//alert("Y= " + Y);
    if(document.forms[0].calendar[0].checked == true)    {
        var A = Math.floor(Y/100);
        var B = Math.floor(A/4);
        var C = 2 - A + B;
    }
    else
        C=0;
//alert("C= " + C);
    var E = Math.floor(365.25*(Y + 4716));
//alert("E= " + E);
    var F = Math.floor(30.6001*(M + 1));
//alert("F= " + F);
    var julianday = C + eval(D) + E + F - 1524.5;

Which appears to be consistent with Dieter's link to Wikipedia:
Quote:wenn Monat > 2 dann Y = Jahr, M = Monat
sonst Y = Jahr-1, M = Monat+12

D = Tag

H = Stunde/24 + Minute/1440 + Sekunde/86400

wenn TT.MM.YYYY >= 15.10.1582
dann gregorianischer Kalender: A = Int(Y/100), B = 2 - A + Int(A/4)

wenn TT.MM.YYYY <= 04.10.1582
dann julianischer Kalender: B = 0

sonst Fehler: Das Datum zwischen dem 04.10.1582 und dem 15.10.1582 existiert nicht.
Auf den 04.10.1582 (julianischer Kalender) folgte
unmittelbar der 15.10.1582 (gregorianischer Kalender).

JD = Int(365,25*(Y+4716)) + Int(30,6001*(M+1)) + D + H + B - 1524,5
Except that the variable H for hours is missing.

And that's what I got for 2000-01-01 using Python:
Code:
>>> Y, M, D = 2000 - 1, 1 + 12, 1
>>> A = Y/100
>>> B = A/4
>>> C = 2-A+B
>>> E = int(365.25 * (Y+4716))
>>> F = int(30.6001*(M+1))
>>> JD= C+D+E+F-1524.5
>>> JD
2451544.5

But I have to admit that I still don't really understand the difference between Julian Date and Julian Day Number.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
01-15-2019, 12:43 PM (This post was last modified: 01-15-2019 01:14 PM by Albert Chan.)
Post: #8
RE: (12C) Gregorian Date to Julian Day Number and vice versa
(01-14-2019 09:28 PM)Thomas Klemm Wrote:  But I have to admit that I still don't really understand the difference between Julian Date and Julian Day Number.

Based on Astronomy Answers: Julian Day Number
Chronological Julian Date assumed timer started at 00:00:00 LT, instead of 12:00:00 UTC

For Jan 1, 2000, 00:00:00 UTC, time zone TZ = +6 hrs = 0.25 day

JDN = floor(JD) = floor(2451544.5) = 2451544
CJDN = floor(CJD) = floor(JD + TZ + 0.5) = 2451545

If true, what post #1 calculated is actually Chronological Julian Day Number.
Find all posts by this user
Quote this message in a reply
01-16-2019, 05:13 PM
Post: #9
RE: (12C) Gregorian Date to Julian Day Number and vice versa
From HP-12C owner's manual, valid date range = Oct 15, 1582 to Nov 25, 4046

It is trivial to extend the range, using the fact Gregorian calendar cycle of 400 years.
Average days per year = (400*365 + 97 leap years)/400= 146097/400 = 365.2425

Example:

CJDN(Dec 31, 9999)
= CJDN(Dec 31, 1999) + 8000 years
= (2451545-1) + 8000 * 365.2425
= 5373484

Since 146097 % 7 = 0, weekday(Dec 31, 9999) = weekday(Dec 31, 1999) = Friday
Find all posts by this user
Quote this message in a reply
01-16-2019, 05:42 PM (This post was last modified: 01-16-2019 05:43 PM by Dieter.)
Post: #10
RE: (12C) Gregorian Date to Julian Day Number and vice versa
(01-16-2019 05:13 PM)Albert Chan Wrote:  From HP-12C owner's manual, valid date range = Oct 15, 1582 to Nov 25, 4046

It is trivial to extend the range, using the fact Gregorian calendar cycle of 400 years.

I wonder why the date range is limited to this 900.000 day period at all. The usual formula does not cause such limitations.

(01-16-2019 05:13 PM)Albert Chan Wrote:  CJDN(Dec 31, 9999)
= CJDN(Dec 31, 1999) + 8000 years
= (2451545-1) + 8000 * 365.2425
= 5373484

This is also the result with "the usual formula". The WP34s, for instance, handles this easily.

(01-16-2019 05:13 PM)Albert Chan Wrote:  Since 146097 % 7 = 0

I assume you mean 146097 mod 7 here.#-)

But then... who cares about dates in 4000 or 8000 years? The Julian calendar was in use for about 1600 years (but it still is, here and there), and the current Gregorian system is not moch more than 400 years old. So who knows what calendar will be in use several thousand years from now?

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 5 Guest(s)