Created
August 30, 2023 05:51
-
-
Save JonathonAshworth/d49b4831622a33b247edd0b95c50f073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jdn conversions for date arithmetic/comparison | |
const intDiv = (l,r) => ~~(l/r); // integer division | |
const jdnFromIso = iso => { | |
const [y, m, d] = iso.split('-').map(s => parseInt(s, 10)); | |
return intDiv(1461 * (y + 4800 + intDiv(m - 14, 12)), 4) | |
+ intDiv(367 * (m - 2 - 12 * intDiv(m - 14, 12)), 12) | |
- intDiv(3 * intDiv(y + 4900 + intDiv(m - 14, 12), 100), 4) | |
+ d - 32075; | |
}; | |
const isoFromJdn = jdn => { | |
const f = jdn + 1401 + intDiv(intDiv(4 * jdn + 274277, 146097) * 3, 4) - 38; | |
const e = 4 * f + 3; | |
const g = intDiv(e % 1461, 4); | |
const h = 5 * g + 2; | |
const day = intDiv(h % 153, 5) + 1; | |
const month = (intDiv(h, 153) + 2) % 12 + 1; | |
const year = intDiv(e, 1461) - 4716 + intDiv(14 - month, 12); | |
return year.toString().padStart(4, '0') + '-' + month.toString().padStart(2, '0') + '-' + day.toString().padStart(2, '0'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment