# Calculate the phases of the moon

#### Ryan2003

##### New member
Tutorial Author
This code will calculate the various phases of the moon.
Good for the northern hemisphere.
Good for dates from 15 Aug. 1939 and newer.
Good for avoiding being out in public on a full moon when werewolves might be out. Code:
``````Strict

' Calculates the phase of the moon from a date
' Formula/description is from here : https://www.subsystems.us/free-resources.html
' // new moon at 14/08/1939
' = Julian Date 2429490.16387
' Example date given below
' Read output in the console window (HTML5)

Import mojo

'-----------------------------------------------------------------
Class myClass Extends App

Field A:Int
Field B:Int
Field C:Int
Field E:Int
Field F:Int
Field JD:Float

Field DaysSinceNew:Float
Field NewMoons:Float
Field Fraction:Float
Field DaysInToCycle:Float    ' days since new moon

Field Phase:String

' Example Date
Field Year:Int = 1939
Field Month:Int = 10
Field Day:Int = 30

'-----------------------------------------------------------------
Method OnCreate:Int()

If Month = 1 OR Month = 2 Then
Year = Year - 1
Month = Month  + 12
EndIf

A = Int(Year/100)
B = Int(A/4)
C = 2 - A + B
E = Int(365.25 * (Year + 4716))
F = Int(30.6001 * (Month + 1))
JD = C + Day + E + F - 1524.5

DaysSinceNew = JD - 2429490.16387

NewMoons = DaysSinceNew / 29.53

Fraction = NewMoons - Int(NewMoons)

DaysInToCycle = Fraction * 29.53

If (DaysInToCycle > 0) AND (DaysInToCycle) <= 1 Then
Phase = "new"
ElseIf DaysInToCycle > 1 AND (DaysInToCycle) <= 6.38264692644 Then
Phase = "waxing cresent"
ElseIf DaysInToCycle > 6.38264692644 AND (DaysInToCycle) <= 8.38264692644 Then
Phase = "first quarter"
ElseIf DaysInToCycle > 8.38264692644 AND (DaysInToCycle) <= 13.76529385288 Then
Phase = "waxing gibbous"
ElseIf DaysInToCycle > 13.76529385288 AND (DaysInToCycle) <= 15.76529385288 Then
Phase = "full"
ElseIf DaysInToCycle > 15.76529385288 AND (DaysInToCycle) <= 21.14794077932 Then
Phase = "waning gibbous"
ElseIf DaysInToCycle > 21.14794077932 AND (DaysInToCycle) <= 23.14794077932 Then
Phase = "last quarter"
ElseIf DaysInToCycle > 23.14794077932 AND (DaysInToCycle) <= 28.53058770576 Then
Phase = "waning crescent"
ElseIf DaysInToCycle > 28.53058770576 AND (DaysInToCycle) <= 29.53058770576 Then
Phase = "new"
EndIf

Print Phase

Return 0
End
'-----------------------------------------------------------------
Method OnUpdate:Int()

Return 0
End
'-----------------------------------------------------------------
Method OnRender:Int()
' here you render your app

Return 0
End
End

'-----------------------------------------------------------------
Function Main:Int()
New myClass
Return 0
End``````

• • MikeHart and Phil7