Skip to content

Instantly share code, notes, and snippets.

@unleashy
Created May 14, 2024 05:10
Show Gist options
  • Save unleashy/f65f522c6a637aa5af55a98aca62879a to your computer and use it in GitHub Desktop.
Save unleashy/f65f522c6a637aa5af55a98aca62879a to your computer and use it in GitHub Desktop.
Julian day → Gregorian calendar in Temporal
function fromJulianDay(jd) {
const Y = 4716;
const J = 1401;
const M = 2;
const N = 12;
const R = 4;
const P = 1461;
const V = 3;
const U = 5;
const S = 153;
const W = 2;
const B = 274277;
const C = -38;
let f = jd + J + Math.floor((Math.floor((4 * jd + B) / 146097) * 3) / 4) + C;
let e = R * f + V;
let g = Math.floor((e % P) / R);
let h = U * g + W;
let d = Math.floor((h % S) / U) + 1;
let m = (Math.floor(h / S) + M) % N + 1;
let y = Math.floor(e / P) - Y + Math.floor((N + M - m) / N);
let pd = new Temporal.PlainDateTime(y, m, d, 12);
let frac = (jd - Math.floor(jd)) * 86400;
frac = Math.round((frac + Number.EPSILON) * 1000000 / 1000000)
return pd.add(`PT${frac}S`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment