Skip to content

Instantly share code, notes, and snippets.

@phothinmg
Last active April 10, 2024 09:29
Show Gist options
  • Save phothinmg/9f8d3ca9207cc98072ae7cc8822e29ee to your computer and use it in GitHub Desktop.
Save phothinmg/9f8d3ca9207cc98072ae7cc8822e29ee to your computer and use it in GitHub Desktop.
Converts a given date and time in the Gregorian calendar to the Julian day number.
// Convert Gregorian Date and Time to Julian
// @2024 PHO THIN MAUNG
function toJulian({
year,
month,
day,
hour = 12,
minute = 0,
second = 0,
tz = 0,
}) {
const df = (hour - 12) / 24 + minute / 1440 + second / 86400;
const tzone = tz / 24;
const a = Math.floor((month - 3) / 12);
const x4 = year + a;
const x3 = Math.floor(x4 / 100);
const x2 = x4 % 100;
const x1 = month - 12 * a - 3;
const jdn =
Math.floor((146097 * x3) / 4) +
Math.floor((36525 * x2) / 100) +
Math.floor((153 * x1 + 2) / 5) +
day +
1721119;
const jd = jdn + df;
const cjd = jd + 0.5 + tzone;
const cjdn = Math.floor(cjd);
return { jd, jdn, cjd, cjdn };
}
console.log(
toJulian({
year: 2024,
month: 1,
day: 1,
hour: 6,
minute: 16,
second: 18,
tz: 6.5,
})
);
/*
{
jd: 2460310.7613194445,
jdn: 2460311,
cjd: 2460311.532152778,
cjdn: 2460311
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment