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:
- New Moon: The moon is between the Earth and the Sun, so the side of the moon that is in shadow faces the Earth.
- Waxing Crescent: The moon starts to move away from the sun, and a sliver of light begins to appear.
- First Quarter: The moon is at a 90-degree angle from the Earth and Sun, showing half of its illuminated side.
- Waxing Gibbous: More than half of the moon is illuminated as it moves towards being full.
- Full Moon: The Earth is between the moon and the Sun, resulting in the moon's entire face being illuminated.
- Waning Gibbous: Following the full moon, the illuminated portion starts to decrease.
- Last Quarter: Similar to the first quarter, but the opposite half is illuminated.
- Waning Crescent: The sliver of light decreases until it becomes a new moon again.
Step-by-step phase calculation
- Julian Day Number: Convert the Gregorian date to a JDN. With , and :
- 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:The fractional part is the phase. Multiply it by 29.53058867 to get the moon's age in days since the last new moon.
- 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
- Julian Day Number: a = 0, y = 6823, m = 7, which gives JDN = 2460234.
- Lunations: `(2460234 - 2451550.1) / 29.53058867` = 294.0646, so the fractional part is 0.0646 (about 1.9 days after new moon).
- Phase: 0.0646 falls between 0 and 0.25, so the moon is a waxing crescent.
Phase lookup table
| Phase Name | Moon Age (Fraction) | Description |
| New Moon | 0 | Moon between Earth and Sun. |
| Waxing Crescent | 0 to 0.25 | Increasing sliver of light. |
| First Quarter | 0.25 | Right half of the moon lit. |
| Waxing Gibbous | 0.25 to 0.5 | More than half is illuminated. |
| Full Moon | 0.5 | Earth between moon and Sun. |
| Waning Gibbous | 0.5 to 0.75 | Light decreases post full moon. |
| Last Quarter | 0.75 | Left half of the moon lit. |
| Waning Crescent | 0.75 to 1 | Sliver 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.
Related reading
- More efficient algorithm to find OR of two sets
- Most common subset of size k
- Most common substring of length X
- Most efficient algorithm for merging sorted IEnumerableT
- Most efficient code for the first 10000 prime numbers?
- Most efficient method of generating a random number with a fixed number of bits set
- Most efficient sorting algorithm for a large set of numbers
- Most efficient way of randomly choosing a set of distinct integers

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 courseData 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.