Introduction

I wrote an article to calculate and display the Moon's phase using C#. Now, this article shows how to calculate and display the Moon's phase using JavaScript; the code for calculating the Moon's age in days (approximately).
moonage.gif
The code
  1. <!-- Moon phase by: Mostafa Kaisoun ([email protected]) -->
  2. <html>
  3. <head>
  4. <title >Moon age</title>
  5. <script type="text/javascript">
  6. var n=31;
  7. var feb;
  8. var leap;
  9. months = ["January","February","March","April","May","June",
  10. "July","August","September","October","November","December"];
  11. function Start()
  12. {
  13. FillYearCombo();
  14. FillMonthCombo();
  15. FillDayCombo();
  16. <!-- Display moon today -->
  17. getAge(MyForm);
  18. }
  19. <!-- Fill first combo with years from 2000 to 2020 -->
  20. function FillYearCombo()
  21. {
  22. for (var i=2000;i<2021;i++)
  23. {
  24. var optYear = new Option(i,i);
  25. document.getElementById("cmbYears").add(optYear, undefined);
  26. }
  27. <!-- Set current year to combo -->
  28. var curDate = new Date();
  29. var y = curDate.getFullYear();
  30. MyForm.YearCombo.value=y;
  31. }
  32. <!-- Fill second combo with months array -->
  33. function FillMonthCombo()
  34. {
  35. for (var i=0;i<months.length;i++)
  36. {
  37. var optMonth = new Option(months[i],i+1);
  38. document.getElementById("cmbMonths").add(optMonth, undefined);
  39. }
  40. <!-- Set current month to combo
  41. var curDate = new Date();
  42. var m = curDate.getMonth();
  43. MyForm.MonthCombo.value=m+1;
  44. }
  45. <!-- Fill third combo with days of selected month -->
  46. function FillDayCombo()
  47. {
  48. for (var i=1;i<n+1;i++)
  49. {
  50. var optDay = new Option(i,i);
  51. document.getElementById("cmbDays").add(optDay, undefined);
  52. }
  53. <!-- Set current day to combo -->
  54. var curDate = new Date();
  55. var d = curDate.getDate();
  56. MyForm.DayCombo.value=d;
  57. }
  58. <!-- Test selected year if it is leap or not -->
  59. function TestLeap()
  60. {
  61. var y=parseInt(MyForm.YearCombo.value);
  62. if ((y%4)==0)
  63. {
  64. if ((y%100)==0 && (y%400)!=0)
  65. leap=false;
  66. else
  67. leap=true;
  68. }
  69. else
  70. leap=false;
  71. }
  72. <!-- Get days of selected month then fill third combo again -->
  73. function GetDays(MonthCombo)
  74. {
  75. var m=parseInt(MyForm.MonthCombo.value);
  76. switch(m)
  77. {
  78. case 2:
  79. TestLeap();
  80. if (leap)
  81. feb=29;
  82. else
  83. feb=28;
  84. n=feb;
  85. break;
  86. case 4:
  87. case 6:
  88. case 9:
  89. case 11:
  90. n=30;
  91. break;
  92. default:n=31;
  93. }
  94. document.forms["MyForm"].elements["DayCombo"].options.length=0;
  95. FillDayCombo();
  96. }
  97. <!-- Get days of February month when select year then fill third combo again -->
  98. function FebruaryDays(YearCombo)
  99. {
  100. TestLeap();
  101. if (leap)
  102. feb=29;
  103. else
  104. feb=28;
  105. if(MyForm.MonthCombo.value==2)
  106. {
  107. n=feb;
  108. document.forms["MyForm"].elements["DayCombo"].options.length=0;
  109. FillDayCombo();
  110. }
  111. }
  112. <!-- Calculate moon age -->
  113. function moonage(d, m, y)
  114. {
  115. var j;
  116. var ip, ag;
  117. j = juliandate(d, m, y);
  118. ip = (j + 4.867) / 29.53059;
  119. ip = ip - Math.floor(ip);
  120. if(ip < 0.5)
  121. {
  122. ag = ip * 29.53059 + 29.53059 / 2;
  123. }
  124. else
  125. {
  126. ag = ip * 29.53059 - 29.53059 / 2;
  127. }
  128. ag = Math.floor(ag) + 1;
  129. return ag;
  130. }
  131. <!-- Calculate Julian date -->
  132. function juliandate ( d, m, y )
  133. {
  134. var d, m, y;
  135. var mm, yy;
  136. var k1, k2, k3;
  137. var j;
  138. yy = y - Math.floor((12 - m) / 10);
  139. mm = m + 9;
  140. if (mm >= 12)
  141. {
  142. mm = mm - 12;
  143. }
  144. k1 = Math.floor(365.25 * (yy + 4712));
  145. k2 = Math.floor(30.6001 * mm + 0.5);
  146. k3 = Math.floor(Math.floor((yy / 100) + 49) * 0.75) - 38;
  147. j = k1 + k2 + d + 59;
  148. if (j > 2299160)
  149. {
  150. j = j - k3;
  151. }
  152. return j ;
  153. }
  154. <!-- Display moon age -->
  155. function getAge(form)
  156. {
  157. var imgName;
  158. var d=parseInt(MyForm.DayCombo.value);
  159. var m=parseInt(MyForm.MonthCombo.value);
  160. var y=parseInt(MyForm.YearCombo.value);
  161. var ag=moonage(d, m, y);
  162. if(ag==1)
  163. {
  164. form.age.value=ag.toString()+" day";
  165. }
  166. else
  167. {
  168. form.age.value=ag.toString()+" days";
  169. }
  170. var imgName="images/phase"+ag.toString()+".bmp";
  171. ShowMoonPhase(imgName);
  172. }
  173. <!-- Display moon phase -->
  174. function ShowMoonPhase(imgFile)
  175. {
  176. document["MoonImage"].src=imgFile;
  177. }
  178. </script>
  179. </head>
  180. <body onload="Start();">
  181. <form name="MyForm" method="post">
  182. <h2><p align=center> Moon phase </h2>
  183. <p align=center> by: Mostafa Kaisoun ([email protected])
  184. <p align=center>
  185. Select year:
  186. <select id="cmbYears" name="YearCombo" onchange="FebruaryDays(this);">
  187. </select>
  188. Select month:
  189. <select id="cmbMonths" name="MonthCombo" onchange="GetDays(this);">
  190. </select>
  191. Select day:
  192. <select id="cmbDays" name="DayCombo";>
  193. </select>
  194. <p align=center> Click button to get moon phase:
  195. <input name="moon" value="Moon phase" type=button onClick="getAge(MyForm)";>
  196. <p align=center> <b>Moon age:</b>
  197. <input name="age" readonly size=10>
  198. <p align=center> <b>Moon phase:</b>
  199. <img id="PhaseImage" name="MoonImage" src="images/phase0.bmp";>
  200. </form>
  201. </body>
  202. </html>

Summary

I hope this article is useful. If you have any idea, please tell me.