, and made a few minor tweaks, and it appears to have a few problems. For instance, the moon phase occasionally shows up as "undefined"; I don't know enough about javascript to fix it, please help.
So, I wanted to display the moon phase as part of my custom portal. You can see the page
here (I display it in a sidebar in Firefox with a userstyle defined in the browser, if you're wondering why it's so oddly laid out and ugly).
I did make a few tweaks to this, but mostly just changing the display text (abbreviated day of week, etc.). I also managed to figure out how to round off the "days old" display (because "the moon is 1.6999999999999993 days old" looks ridiculous). Here is the code as I have it:
function getMoonAge(year, month, day) {
d = Math.floor(year/20)
r = year-(d*20) //r is the remainder of (year/20)
while (r>9)
{ r = r-19 }
r = r*11
while (r>29)
{ r = r-30 }
if (month<3)
{ month = month+2 }
r = r+month+day
if (year<100)
{ r = r-4 }
else
{ r = r-8.3 }
while(r>29)
{ r = r-30 }
while(r<0)
{ r = r+30 }
return r
}
function getMoonPhase(moonAge) {
if (moonAge<1) return "New"
if (moonAge<6) return "Waxing Crescent"
if (moonAge<9) return "First Quarter"
if (moonAge<13) return "Waxing Gibbous"
if (moonAge<16) return "Full"
if (moonAge<20) return "Waning Gibbous"
if (moonAge<23) return "Last Quarter"
if (moonAge<25) return "Waning Crescent"
if (moonAge<29) return "Waning Crescent"
if (moonAge<1) return "New"
}
function getMoonPhaseImg(moonAge) {
if (moonAge<1) return "New"
if (moonAge<5) return "Waxing_Crescent"
if (moonAge<9) return "First_Quarter"
if (moonAge<13) return "Waxing_Gibbous"
if (moonAge<16) return "Full"
if (moonAge<20) return "Waning_Gibbous"
if (moonAge<22) return "Last_Quarter"
if (moonAge<25) return "Waning_Crescent"
if (moonAge<29) return "Waning_Crescent"
if (moonAge<30) return "New"
}
// name arrays
monthNames = new Array(13)
monthNames[1] = "Jan"
monthNames[2] = "Feb"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "Sept"
monthNames[10] = "Oct"
monthNames[11] = "Nov"
monthNames[12] = "Dec"
dayNames = new Array(8)
dayNames[1] = "Sun"
dayNames[2] = "Mon"
dayNames[3] = "Tues"
dayNames[4] = "Wed"
dayNames[5] = "Thurs"
dayNames[6] = "Fri"
dayNames[7] = "Sat"
function getLongDate(dateObj) {
theDay = dayNames[dateObj.getDay()+1]
theMonth = monthNames[dateObj.getMonth()+1]
theDate = dateObj.getDate()
return ""+theDay+", "+theMonth+" "+theDate
}
function getNextFull(moonAge) {
currMilSecs = (new Date()).getTime()
daysToGo = 15 - moonAge
while(daysToGo<2)
{ daysToGo = daysToGo+29 }
milSecsToGo = daysToGo*24*60*60*1000
nextMoonTime = currMilSecs+milSecsToGo
nextMoonDate = new Date(nextMoonTime)
return nextMoonDate
}
function getNextNew(moonAge) {
currMilSecs = (new Date()).getTime()
daysToGo = 29 - moonAge
while(daysToGo<2)
{ daysToGo = daysToGo+29 }
milSecsToGo = daysToGo*24*60*60*1000
nextMoonTime = currMilSecs+milSecsToGo
nextMoonDate = new Date(nextMoonTime)
return nextMoonDate
}
The main problem: today, this is what I am seeing:
Wed, Jan 28
The moon is
30 days old
undefined [should be moon phase]
New moon: Wed, Feb 25
Full moon: Wed, Feb 11
Yeah, moon phases generally aren't more than 29 days old, and I don't know why it's doing that. The new moon was the 26th, so it should have gone to the next phase by now. The page I copied it from says:
Today is Wednesday, Jan 28.
The moon is
Waxing Cresent. [sic]
The current lunar month is 1.6999999999999993 days old.
Next new moon: Tuesday, Feb 24.
Next full moon: Tuesday, Feb 10.
So I guess I must have messed it up somewhere, but I don't know how. Probably part of the rounding-off thing I tried, seeing those slightly different dates for the next new and full moons.
It's minor (I know from experience it'll be correct by Friday) and I'm the only one who sees it, but it's still annoying. How do I fix it?
posted by Zed at 11:38 AM on January 28, 2009