Skip to content

Instantly share code, notes, and snippets.

@phothinmg
Created April 10, 2024 09:31
Show Gist options
  • Select an option

  • Save phothinmg/049eb6a9772406bbfacc04a230b34323 to your computer and use it in GitHub Desktop.

Select an option

Save phothinmg/049eb6a9772406bbfacc04a230b34323 to your computer and use it in GitHub Desktop.
Convert Julian Date to Gregorian
// Convert Julian Date to Gregorian
// @2024 PHO THIN MAUNG
function toGregorian(jd) {
const j = Math.floor(jd);
const jdn = Math.round(jd);
const fjdn = jd - j;
const a = 4 * jdn - 6884477;
const x3 = Math.floor(a / 146097);
const r3 = a % 146097;
const b = 100 * Math.floor(r3 / 4) + 99;
const x2 = Math.floor(b / 36525);
const r2 = b % 36525;
const c = 5 * Math.floor(r2 / 100) + 2;
const x1 = Math.floor(c / 153);
const r1 = c % 153;
const cc = Math.floor((x1 + 2) / 12);
const year = 100 * x3 + x2 + cc;
const month = x1 - 12 * cc + 3;
const day = Math.floor(r1 / 5) + 1;
//fraction of jd to hour,minute,second
let xx1;
if(fjdn >= 0.5){
xx1 = (fjdn * 86400 - 43200) / 3600;
}else{
xx1 = (fjdn * 86400 + 43200) / 3600;
}
const hour = Math.floor(xx1);
const xx2 = (xx1 - hour) * 3600;
const xx3 = xx2 / 60;
const minute = Math.floor(xx3);
const second = Math.floor((xx3 - minute) * 60);
return {
year,
month,
day,
hour,
minute,
second,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment