Although this question apparently addresses the topic, it does not tell appropriate factors involving the moon phase.

I found two distinct algorithms. this one and also this one. I am less than a newbie, less than a layman, to astronomy - but I am not to programming, but I would like to skip testing the algorithm for a couple of years before choosing either.

After unsuccessfully finding a comparison of different algorithms, I'd like to know of either of these algorithms is less error-prone, or if there is someone less error prone-to these (I know there is no stuff like a perfect algorithm, but I'd like to have a good one), or having appropriate ways to judge an algorithm.

So my question comes like this:

Do these methods have a small error, enough to accurately calculate the moon phase?


Can you give me the most precise algorithm -or concepts / underlying theory I should know- astronomers would use as of today to calculate the moon phase?

  • $\begingroup$ It isn't easy to calculate the phase of the moon accurately. The two algorithms you linked are rather crude. From a quick glance, they may be accurate for the 4 main phases to within a day or so. $\endgroup$
    – PM 2Ring
    Commented May 9, 2017 at 9:16
  • 1
    $\begingroup$ To get the same results NASA/USNO does, use the CSPICE libraries: naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/index.html There are plenty of good examples in the documentation and plenty of bad ones at github.com/barrycarter/bcapps/blob/master/ASTRO (I've used these to answer several questions on various stackexchanges). $\endgroup$
    – user21
    Commented May 12, 2017 at 3:26

3 Answers 3


The Moon's phase isn't easy to calculate accurately. Fortunately, the 4 main phases (new moon, first quarter, full moon, last quarter) are a bit more stable than the intermediate phases. Good main phase algorithms use a dozen or so sine calculations for the new or full moon, and a similar number for the first or last quarter. Those two algorithms you linked use very simple approximations so their results are rather crude.

The period of the Moon's phase cycle (the synodic month) is stable over the long term, but it drifts considerably over the short term. So algorithms like those in your links are usable over a rather long time span, and they can be useful first approximations to feed to better algorithms, but by themselves they only yield precision results by accident. ;)

The guru of amateur astronomical calculations for bodies in the solar system is Jean Meeus, so if you want quality algorithms make sure they reference Meeus. :)

For seriously accurate ephemerides, especially if you need to cover a wide time span, consider using the work of JPL. You can access their ephemerides at the Horizons Web page; an API, telnet and an email-based interface are also provided.

If you want to do precise astronomical calculations (eg, for eclipse prediction) you'll need to learn about the various time systems used in astronomy, in particular Terrestrial Time and its forerunner, Ephemeris Time.

I see from your SO profile that you're familiar with Python. Good stand-alone Python moon phase code isn't easy to find online, but eventually I did find keturn's py-moon-phase, which is a Python port of the core code of John Walker's rather ancient Moontool, which uses algorithms from Meeus and also from Peter Duffett-Smith. I haven't actually tested John Walker / keturn's code, but it does look rather good (although it uses UTC instead of Ephemeris Time or Terrestrial Time). I have very similar (but rather messy) C code that's generally accurate to half a minute or better over the 20th century.

However, there's a package called PyEphem that can do all sorts of astronomical calculations. James K notes in the comments that PyEphem is now discouraged for new projects. The PyEphem developers recommend Skyfield instead. Astropy can also do a lot of what PyEphem does, and much more.

  • $\begingroup$ Thankyou! I have a lot of code to test, it seems. Let me check them (have +1 meanwhile)... $\endgroup$ Commented May 9, 2017 at 20:29
  • $\begingroup$ Noting that PyEphem is now discouraged for new projects. The PyEphem developers recommend Skyfield instead. Astropy can also do a lot of what PyEphem does, and much more. $\endgroup$
    – James K
    Commented Apr 25, 2021 at 9:50
  • $\begingroup$ Thanks for that update, @James. $\endgroup$
    – PM 2Ring
    Commented Apr 25, 2021 at 10:42

The procedure for finding the phase of the Moon is outlined in The Explanatory Supplement to the Astronomical Almanac, University Science Books, 3rd ed., p. 507. The annual "Astronomical Almanac" tabulates the phases to the nearest minute of UT. A phase occurs when the excess of the apparent geocentric ecliptic longitude of the Moon over the apparent geocentric ecliptic longitude of the Sun is 0, 90, 180, or 270 degrees. That section says "there is no simple prediction formula with which to make a comparison". But that statement would apply to a desired accuracy of 1 minute. If your accuracy need is looser, maybe a usable formula exists, I don't know.

If your need is within a few hundred years of the present, there is a wide range of software to choose from. But the expressions used can deteriorate rapidly outside the intended range of validity. So if you are examining the distant past or future be sure to use software intended for that purpose.


Divide the year by 19 and multiply the remainder by 11. Add the number of complete months since March1 Add the day of the month. Reduce this mod 30. That is the approximate (+/- 1) number of days since the last new moon.

For January and February use the previous year.

  • 1
    $\begingroup$ Hello Jack, thanks for this contribution. The question asks about the accuracy of various algorithms, and as other answers indicate, detailed calculations use multiple trig functions to account for eccentricity, precession and a host of other factors. The OP asked for "most precise algorithm [...] to calculate the moon phase?" $\endgroup$
    – James K
    Commented Aug 20, 2018 at 15:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .