<p>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.</p>
<p>The period of the Moon's phase cycle (the <a href="https://en.wikipedia.org/wiki/Lunar_month#Synodic_month" rel="nofollow noreferrer">synodic month</a>) 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. ;)</p>
<p>The guru of amateur astronomical calculations for bodies in the solar system is <a href="https://en.wikipedia.org/wiki/Jean_Meeus" rel="nofollow noreferrer">Jean Meeus</a>, so if you want quality algorithms make sure they reference Meeus. :)</p>
<p>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 <a href="https://ssd.jpl.nasa.gov/horizons/" rel="nofollow noreferrer">Horizons</a> Web page; an API, telnet and an email-based interface are also provided.</p>
<p>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 <a href="https://en.wikipedia.org/wiki/Terrestrial_Time" rel="nofollow noreferrer">Terrestrial Time</a> and its forerunner, Ephemeris Time.</p>
<p>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 <a href="http://bazaar.launchpad.net/%7Eketurn/py-moon-phase/trunk/annotate/head:/moon.py" rel="nofollow noreferrer">py-moon-phase</a>, which is a Python port of the core code of John Walker's rather ancient <a href="http://www.fourmilab.ch/moontoolw" rel="nofollow noreferrer">Moontool</a>, 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.</p>
<p>However, there's a package called <a href="http://rhodesmill.org/pyephem/index.html" rel="nofollow noreferrer">PyEphem</a> 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 <a href="https://rhodesmill.org/skyfield/" rel="nofollow noreferrer">Skyfield</a> instead. <a href="https://www.astropy.org/" rel="nofollow noreferrer">Astropy</a> can also do a lot of what PyEphem does, and <em>much</em> more.</p>

CC BY-SA 3.0
2017-05-09T20:29:59.613Z
Thankyou! I have a lot of code to test, it seems. Let me check them (have +1 meanwhile)...
Luis Masuelli
CC BY-SA 4.0
2021-04-25T09:50:58.340Z
Noting that PyEphem is now discouraged for new projects.  The PyEphem developers recommend [Skyfield](https://rhodesmill.org/skyfield/) instead.  Astropy can also do a lot of what PyEphem does, and much more.
James K
CC BY-SA 4.0
2021-04-25T10:42:15.527Z
Thanks for that update, @James.
PM 2Ring
CC BY-SA 4.0
2017-05-09T10:12:39.813Z
2023-03-27T07:13:23.570Z
2023-03-27T07:13:23.570Z
PM 2Ring
<p>The procedure for finding the phase of the Moon is outlined in <em>The Explanatory Supplement to the Astronomical Almanac</em>, 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.</p>

<p>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.</p>

CC BY-SA 4.0
2018-08-21T13:20:28.853Z
2018-08-21T13:20:28.853Z
Gerard Ashton
<p>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.</p>

<p>For January and February use the previous year.</p>

CC BY-SA 4.0
2018-08-20T15:41:58.983Z
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?"
James K
CC BY-SA 4.0
2018-08-20T14:57:57.727Z
2018-08-20T14:57:57.727Z
Jack
<p>Although <a href="https://astronomy.stackexchange.com/questions/914/how-can-i-calculate-moon-phase-and-height">this question</a> apparently addresses the topic, it does not tell appropriate factors involving the moon phase.  </p>

<p>I found two distinct algorithms. <a href="https://gist.github.com/miklb/ed145757971096565723" rel="noreferrer">this one</a> and also <a href="https://www.daniweb.com/programming/software-development/code/453788/moon-phase-at-a-given-date-python" rel="noreferrer">this one</a>. 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.</p>

<p>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.</p>

<p>So my <strong>question</strong> comes like this:</p>

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

<p>Or...</p>

<p>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?</p>

CC BY-SA 3.0
2017-05-09T09:16:18.920Z
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.
PM 2Ring
<moon-phases>
What should I consider in order to accurately calculate the moon phase?
Luis Masuelli
# What should I consider in order to accurately calculate the moon phase?

(Asked by: username_0 on 2017-05-08)

Although [this question](https://astronomy.stackexchange.com/questions/914/how-can-i-calculate-moon-phase-and-height) apparently addresses the topic, it does not tell appropriate factors involving the moon phase.

I found two distinct algorithms. [this one](https://gist.github.com/miklb/ed145757971096565723) and also [this one](https://www.daniweb.com/programming/software-development/code/453788/moon-phase-at-a-given-date-python). 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?

Or...

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?

**Tags:** moon-phases
> **Comments:**
> **username_1**: 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.  

---

## 3 answers

### Answer by username_1 on 2017-05-09. Score: 8

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](https://en.wikipedia.org/wiki/Lunar_month#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](https://en.wikipedia.org/wiki/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](https://ssd.jpl.nasa.gov/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](https://en.wikipedia.org/wiki/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](http://bazaar.launchpad.net/%7Eketurn/py-moon-phase/trunk/annotate/head:/moon.py), which is a Python port of the core code of John Walker's rather ancient [Moontool](http://www.fourmilab.ch/moontoolw), 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](http://rhodesmill.org/pyephem/index.html) 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](https://rhodesmill.org/skyfield/) instead. [Astropy](https://www.astropy.org/) can also do a lot of what PyEphem does, and *much* more.

> **Comments:**
> **username_0**: Thankyou! I have a lot of code to test, it seems. Let me check them (have +1 meanwhile)...  
> **username_2**: Noting that PyEphem is now discouraged for new projects.  The PyEphem developers recommend [Skyfield](https://rhodesmill.org/skyfield/) instead.  Astropy can also do a lot of what PyEphem does, and much more.  
> **username_1**: Thanks for that update, @James.  

---

### Answer by username_3 on 2018-08-21. Score: 0

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.


---

### Answer by username_4 on 2018-08-20. Score: -2

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.

> **Comments:**
> **username_2**: 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?"  

---

