These two functions below are short versions that work with the time elapsed in milliseconds since Jan 1, 1970 UTC.
Most languages have routines to produce time in this way, so it may be preferable to the long routine below. Since
almost all libraries have issues dealing with dates before Oct 15, 1582, be very careful trying to modify these
routines to work with dates before then. If you have the time in seconds, rather than milliseconds, change
86400000 to 86400.
function JulianDateFromUnixTime(t){
return (t / 86400000) + 2440587.5;
}
function UnixTimeFromJulianDate(jd){
return (jd-2440587.5)*86400000;
}
JavaScript functions to convert to and from a Julian Date. Algorithms from Astonomical Algorithms (Meeus).
Convert Gregorian Date to Julian Date:
Convert Julian Date to Gregorian Date:
function INT(d){
if(d>0){
return Math.floor(d);
}
if(d==Math.floor(d)){
return d;
}
return Math.floor(d)-1;
}
function gregorianDateToJulianDate(year, month, day, hour, min, sec){
let isGregorian=true;
if(year<1582 || (year == 1582 && (month < 10 || (month==10 && day < 5)))){
isGregorian=false;
}
if (month < 3){
year = year - 1;
month = month + 12;
}
let b = 0;
if (isGregorian){
let a = INT(year / 100.0);
b = 2 - a + INT(a / 4.0);
}
let jd=INT(365.25 * (year + 4716)) + INT(30.6001 * (month + 1)) + day + b - 1524.5;
jd+=hour/24.0;
jd+=min/24.0/60.0;
jd+=sec/24.0/60.0/60.0;
return jd;
}
function julainDateToGregorian(jd){
let temp=jd+.5;
let Z=Math.trunc(temp);
let F=temp-Z;
let A=Z;
if(Z>=2299161){
let alpha=INT((Z-1867216.25)/36524.25);
A=Z+1+alpha-INT(alpha/4);
}
let B=A+1524;
let C=INT((B-122.1)/365.25);
let D=INT(365.25*C);
let E=INT((B-D)/30.6001);
let day=B-D-INT(30.6001*E)+F;
let month=E-1;
if(E>13){
month=E-13;
}
let year=C-4716;
if(month<3){
year=C-4715;
}
let hour=Math.trunc(F*24);
F-=hour/24;
let minute=Math.trunc(F*60*24);
F-=minute/(60*24);
seconds=Math.round(F*60*60*24*1000)/1000;
return [year,month,day,hour,minute,seconds];
}