Julian Date Number/Gregorian Calendar Date Conversions - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP Prime Software Library (/forum-15.html) +--- Thread: Julian Date Number/Gregorian Calendar Date Conversions (/thread-12184.html) Julian Date Number/Gregorian Calendar Date Conversions - Eddie W. Shore - 01-14-2019 01:21 AM HP Prime Program JD: Gregorian Date to Julian Date Number Arguments: four digit year, month, day JD(y,m,d) Code: ``` EXPORT JD(y,m,d) BEGIN // Gregorian to Julian // year, month, day // 2019-01-12 EWS //  Wikipedia LOCAL x0:=IP((m-14)/12); LOCAL x1:=IP(((1461*(y+4800+x0))/4)); LOCAL x3:=IP(367*(m-2-12*x0)/12); LOCAL x5:=IP((y+4900+x0)/100); LOCAL x6:=IP(3*x5/4); LOCAL j:=x1+x3-x6+d-32075; RETURN j; END;``` HP Prime Program GREG: Julian Date Number to Gregorian Date Argument: Julian Date Number GREG(j) Code: ```EXPORT GREG(j) BEGIN // Julian to Gregorian // Wikipedia // 2019-01-12 EWS LOCAL x1:=IP((4*j+274277)/146097); LOCAL x2:=IP(x1*3/4); LOCAL f:=j+1401+x2-38; LOCAL E:=4*f+3; LOCAL G:=IP((E MOD 1461)/4); LOCAL H:=5*G+2; LOCAL D:=IP((H MOD 153)/5)+1; LOCAL M:=((IP(H/153)+2) MOD 12)+1; LOCAL Y:=IP(E/1461)-4716+IP((12+2-M)/12); RETURN {Y,M,D}; END;``` Examples Gregorian Date: 1988, October 31 JD: 2447466 Gregorian Date: 1999, January 11 JD: 2451190 Gregorian Date: 2017, March 21 JD: 2457834 Source: "Julian Day" Wikipedia. Edited (when retrieved) November 19, 2018. Retrieved January 11, 2019. https://en.wikipedia.org/wiki/Julian_day Blog post link: https://edspi31415.blogspot.com/2019/01/hp-prime-ti-84-plus-and-casio-fx-cg-50.html RE: Julian Date Number/Gregorian Calendar Date Conversions - Dieter - 01-14-2019 12:01 PM (01-14-2019 01:21 AM)Eddie W. Shore Wrote:  HP Prime Program JD: Gregorian Date to Julian Date Number Arguments: four digit year, month, day JD(y,m,d) Eddie, there is no need to calculate this manually with lots of formulas. You can do this with the Prime's built-in calendar functions: For the Julian day number simply determine the date difference to 1582-10-15 and add 2299161. For the Gregorian date subtract 2299161 from the Julian day number and add this number of days to 1582-10-15. You will know the correct Prime syntax for this. ;-) Edit: I have just posted a short 12C program using this approach in the General Software Library. In both cases the date may not be earlier than Oct 15, 1982. This is the first day of the Gregorian calendar, earlier Gregorian dates simply do not exist. If you run your program with an earlier date the result will be invalid – the program works as if the Gregorian calendar existed before the mentioned date (proleptic calendar). I don't know how the Prime behaves in this regard. Does it return an error for earlier dates? (01-14-2019 01:21 AM)Eddie W. Shore Wrote:  Examples Gregorian Date: 1998, October 31 JD: 2447466 Sorry, that's wrong. The JDN for this date is 2451118. 2447466 is the JDN of 31 Oct 1988. Dieter RE: Julian Date Number/Gregorian Calendar Date Conversions - Joe Horn - 01-14-2019 11:13 PM (01-14-2019 12:01 PM)Dieter Wrote:  I don't know how the Prime behaves in this regard. Does it return an error for earlier dates? Yes. It says "Error: Invalid input" for date inputs (or results) outside the range [1582.1015, 9999.1231]. RE: Julian Date Number/Gregorian Calendar Date Conversions - Eddie W. Shore - 01-16-2019 02:48 PM Dieter, I meant 1988, not 1998. Typo on my end. The post was on my blog (see the link in the original post) is for three calculators, not just for the HP Prime. I wanted to get an algorithm down to include code for any calculators that don't have the wonderful DDAYS and DATEADD/DATE+ functions that HP Prime, and most high-end HP calculators have. Quite frankly, the DDAYS and DATEADD should be on every calculator. In future programs, especially on the HP Prime, I will use these functions. The DDAYS function is one of my most used and favorite functions! RE: Julian Date Number/Gregorian Calendar Date Conversions - cdmackay - 01-16-2019 08:18 PM (01-16-2019 02:48 PM)Eddie W. Shore Wrote:  Quite frankly, the DDAYS and DATEADD should be on every calculator. In future programs, especially on the HP Prime, I will use these functions. Interesting that the Prime manual refers only to DateDays (the Finance app function), and DELTADAYS (Catalog) and not DDAYS. Looks like the latter was renamed but the change didn't make the manual? RE: Julian Date Number/Gregorian Calendar Date Conversions - Albert Chan - 03-07-2019 07:17 PM (01-14-2019 01:21 AM)Eddie W. Shore Wrote:  Gregorian Date: 1999, January 11 JD: 2451190 Gregorian Date: 2017, March 21 JD: 2457834 If we also know what weekday the date is, we can get JDN easily: Using Rata Die calendar days (proleptic Gregorian date Jan 1, 0001 (Monday) as day 1) RD ≈ round(((y-1) + (m-1)/12) * 365.2425) + (d-1) max error ±2 days, removed by matching actual weekdays. JDN = 1721425 + RD A quick way to do modulo 7, using 10 ≡ 3, 10² ≡ 2, 10³ ≡ -1 (mod 7) Example: 123456789 (mod 7) ≡ 123 - 456 + 789 ≡ 456 ≡ 2*4 + 3*5 + 6 ≡ 1 + 1 - 1 ≡ 1 For 1999, January 11, Monday: RD ≈ round((1998 + 0/12) * 365.2425) + 10 ≈ 729765 729765 (mod 7) ≡ 765 - 729 ≡ 36 ≡ 1 (matched Monday) -> JDN = 1721425 + 729765 = 2451190 For 2017, March 21, Tuesday: RD ≈ round((2016 + 2/12) * 365.2425) + 20 ≈ 736410 736410 (mod 7) ≡ 410 - 736 ≡ 2(4-7) + 3(1-3) - 6 ≡ 1 + 1 + 1 ≡ 3 (should be Tuesday) -> RD = 736410 - 1 = 736409 -> JDN = 1721425 + 736409 = 2457834