Skip to content

Instantly share code, notes, and snippets.

@emceeaich
Forked from Dither/moon_phase.js
Last active October 11, 2021 18:47
Show Gist options
  • Save emceeaich/3cd273cae1dc688786577a0a7cce8424 to your computer and use it in GitHub Desktop.
Save emceeaich/3cd273cae1dc688786577a0a7cce8424 to your computer and use it in GitHub Desktop.

Revisions

  1. emceeaich revised this gist Oct 11, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion moon_phase.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    function moon_phase(date) { // ported from http://www.voidware.com/moon_phase.htm
    var year = date.getYear(),
    var year = date.getFullYear(),
    month = date.getMonth(),
    day = date.getDay();

  2. @Dither Dither created this gist Sep 17, 2016.
    32 changes: 32 additions & 0 deletions moon_phase.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    function moon_phase(date) { // ported from http://www.voidware.com/moon_phase.htm
    var year = date.getYear(),
    month = date.getMonth(),
    day = date.getDay();

    if (month < 3) {
    year--;
    month += 12;
    }

    ++month;

    jd = 365.25 * year + 30.6 * month + day - 694039.09; // jd is total days elapsed
    jd /= 29.53; // divide by the moon cycle (29.53 days)
    phase = parseInt(jd, 10); // int(jd) -> phase, take integer part of jd
    jd -= phase; // subtract integer part to leave fractional part of original jd
    phase = Math.ceil(jd * 8); // scale fraction from 0-8 and round by adding 0.5
    phase = phase & 7; // 0 and 8 are the same so turn 8 into 0

    switch (phase) {
    case 0: phase = "New Moon"; break;
    case 1: phase = "Waxing Crescent Moon"; break;
    case 2: phase = "Quarter Moon"; break;
    case 3: phase = "Waxing Gibbous Moon"; break;
    case 4: phase = "Full Moon"; break;
    case 5: phase = "Waning Gibbous Moon"; break;
    case 6: phase = "Last Quarter Moon";
    case 7: phase = "Waning Crescent Moon"; break;
    }
    return phase;
    }
    //document.write(moon_phase(new Date(Date.now())))