Last active
November 2, 2025 13:10
-
-
Save joakim/2320c4b4bd3ea002a9781e70a226096b to your computer and use it in GitHub Desktop.
Converts a Temporal.Instant to a Julian Date. Formatted for readability.
This file contains hidden or 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
| const hoursInDay = 24 | |
| const minutesInDay = 1440 | |
| const secondsInDay = 86400 | |
| const millisecondsInDay = 86400000 | |
| const microsecondsInDay = 86400000000 | |
| const nanosecondsInDay = 86400000000000 | |
| /** | |
| * Converts a Temporal.Instant to a Julian Date (decimal number). | |
| **/ | |
| function instantToJD(instant) { | |
| let { | |
| year, | |
| month, | |
| day, | |
| hour, | |
| minute, | |
| second, | |
| millisecond, | |
| microsecond, | |
| nanosecond, | |
| } = instant.toZonedDateTimeISO('UTC') | |
| // *waves wand* | |
| let a = Math.floor((14 - month) / 12) | |
| let y = year + 4800 - a | |
| let m = month + 12 * a - 3 | |
| let jdn = day + Math.floor((153 * m + 2) / 5) | |
| + 365 * y + Math.floor(y / 4) | |
| - Math.floor(y / 100) | |
| + Math.floor(y / 400) | |
| - 32045 | |
| let fraction = hour / hoursInDay | |
| + minute / minutesInDay | |
| + second / secondsInDay | |
| + millisecond / millisecondsInDay | |
| + microsecond / microsecondsInDay | |
| + nanosecond / nanosecondsInDay | |
| return jdn - 0.5 + fraction | |
| } | |
| let jd = instantToJD(Temporal.Now.instant()) | |
| console.log(jd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment