Created
January 19, 2018 13:55
-
-
Save imlinus/4079d087354a3cd8a52ddf7067093a07 to your computer and use it in GitHub Desktop.
Moon Phase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
Moon.phase('2018', '01', '19'); // returns `quarter-moon` | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original Snippet: https://gist.github.com/endel/dfe6bb2fbe679781948c | |
// Moon.phase('2018', '01', '19'); | |
var Moon = { | |
phase: function(year, month, day) { | |
var c = e = jd = b = 0; | |
if(month < 3) { | |
year--; | |
month += 12; | |
} | |
++month; | |
c = 365.25 * year; | |
e = 30.6 * month; | |
jd = c + e + day - 694039.09; // jd is total days elapsed | |
jd /= 29.5305882; // divide by the moon cycle | |
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 | |
switch(b) { | |
case 0: return 'new-moon'; break; | |
case 1: return 'waxing-crescent-moon'; break; | |
case 2: return 'quarter-moon'; break; | |
case 3: return 'waxing-gibbous-moon'; break; | |
case 4: return 'full-moon'; break; | |
case 5: return 'waning-gibbous-moon'; break; | |
case 6: return 'last-quarter-moon'; break; | |
case 7: return 'waning-crescent-moon'; break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment