Skip to content

phothinmg/gregorian-julian.js

Repository files navigation

Converting Julian and Gregorian dates to each other.


ESM only

This package assumes use of the Gregorian calendar and only works correctly for dates after 1858.

Install

npm i gregorian-julian.js
pnpm i gregorian-julian.js
yarn add gregorian-julian.js

Calculates the Julian Day (JD) for a given date.

Before diving into the code, let's understand how to calculate the Julian Day (JD) for a given Gregorian date. The following JavaScript function demonstrates this conversion using the gregorian-julian.js library.

import { toJD } from "gregorian-julian.js";
function calculateJulianDay(year, month, day) {
  try {
    const julianDay = toJD(year, month, day);
    console.log(julianDay);
    return julianDay;
  } catch (error) {
    console.error("Error converting date to Julian Day:", error);
  }
}
const julianDay = calculateJulianDay(2022, 10, 15);
// Output: 2459867.5

Converts a Julian Day (JD) to a Gregorian date and time

Now, let's see how we can convert a Julian Day (JD) back to a Gregorian date. The following function uses the gregorian-julian.js library to perform this conversion and prints the result.

import { toGregorian } from "gregorian-julian.js";
function convertJDToGregorian(jd) {
  const result = toGregorian(jd);
  console.log(`Converted Date: ${result.year}-${result.month}-${result.day} ${result.hour}:${result.minute}:${result.second}`);
  return result;
}
const jd = 2459449.5;
const gregorianDate = convertJDToGregorian(jd);
// Output: Converted Date: 2021-8-23 0:0:0

Test and Coverage

Tested with Jest and checked with online converter of aavso, https://www.aavso.org/jd-calculator.

The negative values of JD, before 1 Jan -4712, are considered experimental.

Coverage report

Functions Coverage Badge Branches Coverage Badge Lines Coverage Badge Statements Coverage Badge