Skip to content

Instantly share code, notes, and snippets.

@GhostToast
Last active February 28, 2020 13:05
Show Gist options
  • Save GhostToast/0e97537e2a2786656a9b71475c722704 to your computer and use it in GitHub Desktop.
Save GhostToast/0e97537e2a2786656a9b71475c722704 to your computer and use it in GitHub Desktop.
Moon Phaser
function getMoonPhase(year, month, day){
var c = e = jd = b = 0;
if (month < 3) {
year--;
month += 12;
}
++month;
var c = 365.25 * year;
var e = 30.6 * month;
var jd = c + e + day - 694039.09; //jd is total days elapsed
jd /= 29.5305882; //divide by the moon cycle
var b = parseInt(jd); //int(jd) -> b, take integer part of jd
jd -= b; //subtract integer part to leave fractional part of original jd
b = Math.round(jd * 8); //scale fraction from 0-8 and round
if (b >= 8 ) {
b = 0; //0 and 8 are the same so turn 8 into 0
}
var phases = [
'New Moon',
'Waxing Crescent',
'First Quarter',
'Waxing Gibbous',
'Full Moon',
'Waning Gibbous',
'Last Quarter',
'Waning Crescent'
];
return phases[b];
}
return getMoonPhase([year],[month_n],[day_n]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment