Here is a simple code that can be used to determine the current moon phase:
import datetime
def get_moon_phase():
# Get the current date and time
now = datetime.datetime.now()
# Calculate the moon phase using the current date and time
# This calculation is based on the formulas provided by the
# United States Naval Observatory
# (https://aa.usno.navy.mil/faq/docs/moon_phases.php)
age_days = now - datetime.datetime(2001, 1, 1)
lunations = age_days.days / 29.530588853
phase = lunations - int(lunations)
if phase < 0.5:
return "New Moon"
elif phase < 0.53:
return "Waxing Crescent"
elif phase < 0.75:
return "First Quarter"
elif phase < 0.78:
return "Waxing Gibbous"
elif phase < 0.99:
return "Full Moon"
else:
return "Waning Gibbous"
# Example usage:
print(get_moon_phase())
This code uses the current date and time to calculate the moon phase,
and returns the phase as a string (e.g. "Full Moon"). The calculation is
based on the formulas provided by the United States Naval Observatory.
To use this code, you can simply call the get_moon_phase() function,
which will return the current moon phase. You can then display the moon
phase to the user or use it in other parts of your code as needed.