Retreiving current Lunar Phase

Posted on 2006-04-17
Last Modified: 2013-12-24
Would anyone here happen to know how I might retrieve the current lunar phase using ColdFusion?  I was thinking of perhaps finding and setting up some kind of database for this, but I'm now thinking that this might not even be necessary.  

Anyone know how I might achieve this programatically?  I've already looked in to using the DateLib UDF, -- but it doesn't seem to handle lunar phases.  Surely, this type of functionality can't be that complicated.  Anyone care to lend a hand?

 - Yvan
Question by:IDEASDesign
    LVL 15

    Expert Comment

    Here is a link:

    Looks like there is a new moon approx. every 29.53058885 days but it is a complicated formula if you want to be exact due to the elliptical orbits.

    Author Comment

    Well, since I'd like to retrieve the other lunar phases as well (not just the full moon phase), .. it seems that I might require some kind of database for this.   Anyone else have any suggestions?

    - Yvan
    LVL 7

    Expert Comment

    The following may help:

    Perhaps you can find something there that you can request, parse, and query for the needed info.
    LVL 7

    Expert Comment

    You can combine the ideas given:  Check the accurate data given at the unno sites against a quick calculation using the given lunar period.  

    Starting at a date-time of a full moon, you get another full moon every period (29.53058885).  

    You will get the new moon at period/2 + period + 2*period ...

    You will get a first quarter moon at period/4 + period + 2*period ...

    You will get a third quarter moon at 3*period/4 + period + 2*period ...

    I haven't done this calculation and checked it against the usno data but I would be surprised if it is not very accurate.  Keep in mind that you will be calculating a date and time of day, but that all you really need is the date of the moon phase.  This gives you lots of room to accept some deviation due to details.
    LVL 7

    Expert Comment

    Keep in mind, however that the accuracy of these calculations will be based on the timezone used for the origin of time.  For all others, the accuracy will be any number of hours incorrect.
    LVL 16

    Expert Comment

    I don't know exactly how to do this, but this library looks like it has the support you are looking for.
    It is opensource and runs in the JRE. If you are using cf MX, you should be able to install the class library and use it... not something I have expertise in doing, other than to know this is entirely possible.

    Author Comment

    I appreciate all the feedback you folks have been giving me. Although I do understand the calculations that have been provided, I suspect that this approach would be a bit too complicated, considering that I'd have to map the span of days to the actual calendar dates.  And in examining the behavior of the Windows executable for the mondtag library, I do not believe that the JAR file equivalient of this would be able to achieve the type of functionionality I am looking for.  

    What would be most ideal is if there was some kind of web service available that would allow me to pass a specific calendar date to it, and have it return the name of the lunar phase to me (New Moon, First Quarter, Full Moon, or Last Quarter) .  Anyone here know if something like this exists?  

    - Yvan

    P.S. The point that was brought up regarding how the timezone effects the lunar phases is perfectly valid.  However, at the point in time, I do not need that level of accuaracy. Thank you for mentioning it, however.  
    LVL 7

    Accepted Solution

    Here is a function that gives the phase of the moon.  Input is a datetime in UTC.  Output is full, last quarter, new, or first quarter.

    I tested it against the usno data and found that it produces good results over at least a 10 year period.  (They only show 10 years of data)

    <!--- index.cfm   Dan Kelly   060418 --->

    <cfif isDefined("form.dateMoon")>
      <cfset dateMoon = form.dateMoon>
      <cfset dateMoon=now()>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "TR/html4/loose.dtd">
    <title>Moon Phase per Date</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <form name="formMoon" id="formMoon" method="post" action="index.cfm" >

    Date:<input type="text" name="dateMoon" value="<cfoutput>#dateMoon#</cfoutput>" >
    <input type="submit" value="Submit">


    <cffunction name="getPhase" returntype="string">
          <cfargument name="dateMoon" type="date" required="yes">
          <cfset dateFull = createDateTime(2000, 1, 21, 4, 40, 0)>
          <cfset periodSeconds = 29.530589*86400>
          <cfset futureSeconds = dateDiff("s", dateFull, arguments.dateMoon)>
          <cfset phase = (futureSeconds mod periodSeconds)/periodSeconds>
          <cfif (phase gte .875) or (phase lt .125)><cfset result="full: #phase#">
          <cfelseif (phase gte .125) and (phase lt .375)><cfset result="last quarter: #phase#">
          <cfelseif (phase gte .375) and (phase lt .625)><cfset result="new: #phase#">
          <cfelse><cfset result="first quarter: #phase#"></cfif>
          <cfreturn result>

    Author Comment

    Perfect!  Thank you so much!  This is exactly what I was looking for.  

    You win :)

    - Yvan

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
    A web service ( is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now