Skip to content

Instantly share code, notes, and snippets.

@pazguille
Last active February 1, 2022 00:55
Show Gist options
  • Save pazguille/7768d80e6923ca48f79050001da5e115 to your computer and use it in GitHub Desktop.
Save pazguille/7768d80e6923ca48f79050001da5e115 to your computer and use it in GitHub Desktop.

Revisions

  1. pazguille revised this gist Feb 1, 2022. 1 changed file with 25 additions and 25 deletions.
    50 changes: 25 additions & 25 deletions getMoonPhase.js
    Original file line number Diff line number Diff line change
    @@ -1,41 +1,41 @@
    const phases = [{
    phase: "new",
    min: 0,
    max: 1
    min: 0,
    max: 1
    },
    {
    phase: "waxing crescent",
    min: 1,
    max: 6.38264692644
    min: 1,
    max: 6.38264692644
    },
    {
    phase: "first quarter",
    min: 6.38264692644,
    max: 8.38264692644
    phase: "first quarter",
    min: 6.38264692644,
    max: 8.38264692644
    }, {
    phase: "waxing gibbous",
    min: 8.38264692644,
    max: 13.76529385288
    phase: "waxing gibbous",
    min: 8.38264692644,
    max: 13.76529385288
    }, {
    phase: "full",
    min: 13.76529385288,
    max: 15.76529385288
    phase: "full",
    min: 13.76529385288,
    max: 15.76529385288
    }, {
    phase: "waning gibbous",
    min: 15.76529385288,
    max: 21.14794077932
    phase: "waning gibbous",
    min: 15.76529385288,
    max: 21.14794077932
    }, {
    phase: "last quarter",
    min: 21.14794077932,
    max: 23.14794077932
    phase: "last quarter",
    min: 21.14794077932,
    max: 23.14794077932
    }, {
    phase: "waning crescent",
    min: 23.14794077932,
    max: 28.53058770576
    phase: "waning crescent",
    min: 23.14794077932,
    max: 28.53058770576
    }, {
    phase: "new",
    min: 28.53058770576,
    max: 29.53058770576
    phase: "new",
    min: 28.53058770576,
    max: 29.53058770576
    }];

    function fmod(dividend, divisor) {
  2. pazguille revised this gist Feb 1, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion getMoonPhase.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    const phases = [{
    phase: "new",
    phase: "new",
    min: 0,
    max: 1
    },
  3. pazguille revised this gist Feb 1, 2022. No changes.
  4. pazguille created this gist Feb 1, 2022.
    71 changes: 71 additions & 0 deletions getMoonPhase.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    const phases = [{
    phase: "new",
    min: 0,
    max: 1
    },
    {
    phase: "waxing crescent",
    min: 1,
    max: 6.38264692644
    },
    {
    phase: "first quarter",
    min: 6.38264692644,
    max: 8.38264692644
    }, {
    phase: "waxing gibbous",
    min: 8.38264692644,
    max: 13.76529385288
    }, {
    phase: "full",
    min: 13.76529385288,
    max: 15.76529385288
    }, {
    phase: "waning gibbous",
    min: 15.76529385288,
    max: 21.14794077932
    }, {
    phase: "last quarter",
    min: 21.14794077932,
    max: 23.14794077932
    }, {
    phase: "waning crescent",
    min: 23.14794077932,
    max: 28.53058770576
    }, {
    phase: "new",
    min: 28.53058770576,
    max: 29.53058770576
    }];

    function fmod(dividend, divisor) {
    let multiplier = 0;
    while(divisor * multiplier < dividend) {
    ++multiplier;
    }
    --multiplier;
    return dividend - (divisor * multiplier);
    }

    function getMoonPhase(date) {
    const $unixdate = Math.round(date.getTime() / 1000);
    const $lunardays = 29.53058770576;
    const $lunarsecs = $lunardays * (24 * 60 *60);
    const $new2000 = Math.round((new Date('2000-01-06T18:14:00.000Z')).getTime() / 1000);
    const $totalsecs = $unixdate - $new2000;
    let $currentsecs = fmod($totalsecs, $lunarsecs);
    if ($currentsecs < 0) {
    $currentsecs += $lunarsecs;
    }
    const $currentfrac = $currentsecs / $lunarsecs;
    const $currentdays = $currentfrac * $lunardays;
    const { phase } = phases.filter(each => ($currentdays >= each.min && $currentdays <= each.max))[0];

    return {
    phase,
    age: $currentdays.toFixed(2),
    lunation: ($currentfrac*100).toFixed(2),
    };
    }

    console.log(getMoonPhase(new Date('2022-02-21T03:00:00.000Z')));