Giter Site home page Giter Site logo

gregorian-julian.js's Introduction

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

gregorian-julian.js's People

Contributors

phothinmg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.