Learn how to a build a cloud-first strategyRegister Now


Retreiving current Lunar Phase

Posted on 2006-04-17
Medium Priority
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
  • 3
  • 2
  • 2
  • +2
LVL 15

Expert Comment

ID: 16471850
Here is a link: http://en.wikipedia.org/wiki/Full_moon_cycle

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

ID: 16472029
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

Expert Comment

ID: 16472687
The following may help:


Perhaps you can find something there that you can request, parse, and query for the needed info.
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.


Expert Comment

ID: 16474425
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.

Expert Comment

ID: 16474490
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

ID: 16474961
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

ID: 16477083
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.  

Accepted Solution

wytcom earned 2000 total points
ID: 16481249
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

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

You win :)

- Yvan

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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