Created
March 25, 2015 16:04
-
-
Save endel/dfe6bb2fbe679781948c to your computer and use it in GitHub Desktop.
Get Moon Phase by Date, written in JavaScript
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
/* | |
* modified from http://www.voidware.com/moon_phase.htm | |
*/ | |
function getMoonPhase(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 | |
} | |
// 0 => New Moon | |
// 1 => Waxing Crescent Moon | |
// 2 => Quarter Moon | |
// 3 => Waxing Gibbous Moon | |
// 4 => Full Moon | |
// 5 => Waning Gibbous Moon | |
// 6 => Last Quarter Moon | |
// 7 => Waning Crescent Moon | |
return b; | |
} | |
console.log(getMoonPhase(2015, 3, 29)); |
teamwork! 💪
Date 16 may 2022 actualy is the day start of fullmoon.
but this script shows that fullmoon start at 13 may 2022..
Is it something wrong?
Full moon was last night here, there are regional differences, but not by that much. You can check the exact time of the full moon for your area with an ephemeris such as this one: https://in-the-sky.org/ephemeris.php?ird=1&irs=1&ima=1&iph=1&iob=1&objtype=1&objpl=Moon&objtxt=the+Moon&tz=0&startday=1&startmonth=6&startyear=2022&interval=1&rows=100
And then cross-reference with the scripts results.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@archipoeta that's magic - thank you!
So to make this solution more robust, it would be best to
parseInt
and make sure that day and month are two digits, and year four digits.It's perhaps less likely that year would get less than two digits. I guess a sensible fallback might be the current century if say it were provided only two digits.