moon phases
lunar cycle
algorithms
astronomy
celestial events

Moon phase algorithm using the lunar cycle length (29.53 days)

Codemia Knowledge Hub ·

To find the moon's phase for a date, convert the date to a Julian Day Number (JDN), subtract the Julian Day of a known new moon (2451550.1), and divide by the length of the lunar cycle, the synodic month of about 29.53 days. The fractional part of the result tells you where the moon is in its cycle: 0 is new moon, 0.5 is full moon.

The eight lunar phases

The phases are the changing appearances of the moon as seen from Earth:

  1. New Moon: The moon is between the Earth and the Sun, so the side of the moon that is in shadow faces the Earth.
  2. Waxing Crescent: The moon starts to move away from the sun, and a sliver of light begins to appear.
  3. First Quarter: The moon is at a 90-degree angle from the Earth and Sun, showing half of its illuminated side.
  4. Waxing Gibbous: More than half of the moon is illuminated as it moves towards being full.
  5. Full Moon: The Earth is between the moon and the Sun, resulting in the moon's entire face being illuminated.
  6. Waning Gibbous: Following the full moon, the illuminated portion starts to decrease.
  7. Last Quarter: Similar to the first quarter, but the opposite half is illuminated.
  8. Waning Crescent: The sliver of light decreases until it becomes a new moon again.

Step-by-step phase calculation

  1. Julian Day Number: Convert the Gregorian date (Y,M,D)(Y, M, D) to a JDN. With a=⌊(14−M)/12⌋a = \left\lfloor (14 - M)/12 \right\rfloor, y=Y+4800−ay = Y + 4800 - a and m=M+12a−3m = M + 12a - 3:
    JDN=D+⌊153m+25⌋+365y+⌊y/4⌋−⌊y/100⌋+⌊y/400⌋−32045JDN = D + \left\lfloor \frac{153m + 2}{5} \right\rfloor + 365y + \left\lfloor y/4 \right\rfloor - \left\lfloor y/100 \right\rfloor + \left\lfloor y/400 \right\rfloor - 32045
  2. Lunations since the epoch: The epoch J.D. = 2451550.1 is a known new moon. Dividing the days elapsed since then by the synodic month gives the number of lunar cycles:
    Lunations=(JDN−2451550.1)/29.53058867\text{Lunations} = (JDN - 2451550.1) / 29.53058867
    The fractional part is the phase. Multiply it by 29.53058867 to get the moon's age in days since the last new moon.
  3. Phase from the fractional part:
    • 0 = New Moon
    • 0 to 0.25 = Waxing Crescent
    • 0.25 = First Quarter
    • 0.25 to 0.5 = Waxing Gibbous
    • 0.5 = Full Moon
    • 0.5 to 0.75 = Waning Gibbous
    • 0.75 = Last Quarter
    • 0.75 to 1 = Waning Crescent

Worked example: October 16, 2023

  1. Julian Day Number: a = 0, y = 6823, m = 7, which gives JDN = 2460234.
  2. Lunations: `(2460234 - 2451550.1) / 29.53058867` = 294.0646, so the fractional part is 0.0646 (about 1.9 days after new moon).
  3. Phase: 0.0646 falls between 0 and 0.25, so the moon is a waxing crescent.

Phase lookup table

Phase NameMoon Age (Fraction)Description
New Moon0Moon between Earth and Sun.
Waxing Crescent0 to 0.25Increasing sliver of light.
First Quarter0.25Right half of the moon lit.
Waxing Gibbous0.25 to 0.5More than half is illuminated.
Full Moon0.5Earth between moon and Sun.
Waning Gibbous0.5 to 0.75Light decreases post full moon.
Last Quarter0.75Left half of the moon lit.
Waning Crescent0.75 to 1Sliver shrinks to darkness.

Uses and accuracy limits

The calculation is used in:

  • Astronomy and science: planning observations, understanding tides, and other celestial calculations.
  • Agriculture: timing planting and harvesting by lunar phase.
  • Cultural practices: lunar calendars for festivals, rituals and religious events.

This method gives quick results, but its accuracy over long periods is limited. More sophisticated algorithms and astronomical models give greater precision.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms