2 * Copyright 2004 Ho Ngoc Duc [http://come.to/duc]. All Rights Reserved.<p>
3 * Permission to use, copy, modify, and redistribute this software and its
4 * documentation for personal, non-commercial use is hereby granted provided that
5 * this copyright notice appears in all copies.
10 /* Discard the fractional part of a number, e.g., INT(3.2) = 3 */
15 /* Compute the (integral) Julian day number of day dd/mm/yyyy, i.e., the number
16 * of days between 1/1/4713 BC (Julian calendar) and dd/mm/yyyy.
17 * Formula from http://www.tondering.dk/claus/calendar.html
19 function jdFromDate(dd, mm, yy) {
21 a = INT((14 - mm) / 12);
24 jd = dd + INT((153*m+2)/5) + 365*y + INT(y/4) - INT(y/100) + INT(y/400) - 32045;
26 jd = dd + INT((153*m+2)/5) + 365*y + INT(y/4) - 32083;
31 /* Convert a Julian day number to day/month/year. Parameter jd is an integer */
32 function jdToDate(jd) {
33 var a, b, c, d, e, m, day, month, year;
34 if (jd > 2299160) { // After 5/10/1582, Gregorian calendar
36 b = INT((4*a+3)/146097);
37 c = a - INT((b*146097)/4);
42 d = INT((4*c+3)/1461);
43 e = c - INT((1461*d)/4);
45 day = e - INT((153*m+2)/5) + 1;
46 month = m + 3 - 12*INT(m/10);
47 year = b*100 + d - 4800 + INT(m/10);
48 return new Array(day, month, year);
51 /* Compute the time of the k-th new moon after the new moon of 1/1/1900 13:52 UCT
52 * (measured as the number of days since 1/1/4713 BC noon UCT, e.g., 2451545.125 is 1/1/2000 15:00 UTC).
53 * Returns a floating number, e.g., 2415079.9758617813 for k=2 or 2414961.935157746 for k=-2
54 * Algorithm from: "Astronomical Algorithms" by Jean Meeus, 1998
57 var T, T2, T3, dr, Jd1, M, Mpr, F, C1, deltat, JdNew;
58 T = k/1236.85; // Time in Julian centuries from 1900 January 0.5
62 Jd1 = 2415020.75933 + 29.53058868*k + 0.0001178*T2 - 0.000000155*T3;
63 Jd1 = Jd1 + 0.00033*Math.sin((166.56 + 132.87*T - 0.009173*T2)*dr); // Mean new moon
64 M = 359.2242 + 29.10535608*k - 0.0000333*T2 - 0.00000347*T3; // Sun's mean anomaly
65 Mpr = 306.0253 + 385.81691806*k + 0.0107306*T2 + 0.00001236*T3; // Moon's mean anomaly
66 F = 21.2964 + 390.67050646*k - 0.0016528*T2 - 0.00000239*T3; // Moon's argument of latitude
67 C1=(0.1734 - 0.000393*T)*Math.sin(M*dr) + 0.0021*Math.sin(2*dr*M);
68 C1 = C1 - 0.4068*Math.sin(Mpr*dr) + 0.0161*Math.sin(dr*2*Mpr);
69 C1 = C1 - 0.0004*Math.sin(dr*3*Mpr);
70 C1 = C1 + 0.0104*Math.sin(dr*2*F) - 0.0051*Math.sin(dr*(M+Mpr));
71 C1 = C1 - 0.0074*Math.sin(dr*(M-Mpr)) + 0.0004*Math.sin(dr*(2*F+M));
72 C1 = C1 - 0.0004*Math.sin(dr*(2*F-M)) - 0.0006*Math.sin(dr*(2*F+Mpr));
73 C1 = C1 + 0.0010*Math.sin(dr*(2*F-Mpr)) + 0.0005*Math.sin(dr*(2*Mpr+M));
75 deltat= 0.001 + 0.000839*T + 0.0002261*T2 - 0.00000845*T3 - 0.000000081*T*T3;
77 deltat= -0.000278 + 0.000265*T + 0.000262*T2;
79 JdNew = Jd1 + C1 - deltat;
83 /* Compute the longitude of the sun at any time.
84 * Parameter: floating number jdn, the number of days since 1/1/4713 BC noon
85 * Algorithm from: "Astronomical Algorithms" by Jean Meeus, 1998
87 function SunLongitude(jdn) {
88 var T, T2, dr, M, L0, DL, L;
89 T = (jdn - 2451545.0 ) / 36525; // Time in Julian centuries from 2000-01-01 12:00:00 GMT
91 dr = PI/180; // degree to radian
92 M = 357.52910 + 35999.05030*T - 0.0001559*T2 - 0.00000048*T*T2; // mean anomaly, degree
93 L0 = 280.46645 + 36000.76983*T + 0.0003032*T2; // mean longitude, degree
94 DL = (1.914600 - 0.004817*T - 0.000014*T2)*Math.sin(dr*M);
95 DL = DL + (0.019993 - 0.000101*T)*Math.sin(dr*2*M) + 0.000290*Math.sin(dr*3*M);
96 L = L0 + DL; // true longitude, degree
98 L = L - PI*2*(INT(L/(PI*2))); // Normalize to (0, 2*PI)
102 /* Compute sun position at midnight of the day with the given Julian day number.
103 * The time zone if the time difference between local time and UTC: 7.0 for UTC+7:00.
104 * The function returns a number between 0 and 11.
105 * From the day after March equinox and the 1st major term after March equinox, 0 is returned.
106 * After that, return 1, 2, 3 ...
108 function getSunLongitude(dayNumber, timeZone) {
109 return INT(SunLongitude(dayNumber - 0.5 - timeZone/24)/PI*6);
112 /* Compute the day of the k-th new moon in the given time zone.
113 * The time zone if the time difference between local time and UTC: 7.0 for UTC+7:00
115 function getNewMoonDay(k, timeZone) {
116 return INT(NewMoon(k) + 0.5 + timeZone/24);
119 /* Find the day that starts the luner month 11 of the given year for the given time zone */
120 function getLunarMonth11(yy, timeZone) {
121 var k, off, nm, sunLong;
122 //off = jdFromDate(31, 12, yy) - 2415021.076998695;
123 off = jdFromDate(31, 12, yy) - 2415021;
124 k = INT(off / 29.530588853);
125 nm = getNewMoonDay(k, timeZone);
126 sunLong = getSunLongitude(nm, timeZone); // sun longitude at local midnight
128 nm = getNewMoonDay(k-1, timeZone);
133 /* Find the index of the leap month after the month starting on the day a11. */
134 function getLeapMonthOffset(a11, timeZone) {
136 k = INT((a11 - 2415021.076998695) / 29.530588853 + 0.5);
138 i = 1; // We start with the month following lunar month 11
139 arc = getSunLongitude(getNewMoonDay(k+i, timeZone), timeZone);
143 arc = getSunLongitude(getNewMoonDay(k+i, timeZone), timeZone);
144 } while (arc != last && i < 14);
148 /* Comvert solar date dd/mm/yyyy to the corresponding lunar date */
149 function convertSolar2Lunar(dd, mm, yy, timeZone) {
150 var k, dayNumber, monthStart, a11, b11, lunarDay, lunarMonth, lunarYear, lunarLeap;
151 dayNumber = jdFromDate(dd, mm, yy);
152 k = INT((dayNumber - 2415021.076998695) / 29.530588853);
153 monthStart = getNewMoonDay(k+1, timeZone);
154 if (monthStart > dayNumber) {
155 monthStart = getNewMoonDay(k, timeZone);
157 //alert(dayNumber+" -> "+monthStart);
158 a11 = getLunarMonth11(yy, timeZone);
160 if (a11 >= monthStart) {
162 a11 = getLunarMonth11(yy-1, timeZone);
165 b11 = getLunarMonth11(yy+1, timeZone);
167 lunarDay = dayNumber-monthStart+1;
168 var diff = INT((monthStart - a11)/29);
170 lunarMonth = diff+11;
171 if (b11 - a11 > 365) {
172 var leapMonthDiff = getLeapMonthOffset(a11, timeZone);
173 if (diff >= leapMonthDiff) {
174 lunarMonth = diff + 10;
175 if (diff == leapMonthDiff) {
180 if (lunarMonth > 12) {
181 lunarMonth = lunarMonth - 12;
183 if (lunarMonth >= 11 && diff < 4) {
186 return new Array(lunarDay, lunarMonth, lunarYear, lunarLeap);
189 /* Convert a lunar date to the corresponding solar date */
190 function convertLunar2Solar(lunarDay, lunarMonth, lunarYear, lunarLeap, timeZone) {
191 var k, a11, b11, off, leapOff, leapMonth, monthStart;
192 if (lunarMonth < 11) {
193 a11 = getLunarMonth11(lunarYear-1, timeZone);
194 b11 = getLunarMonth11(lunarYear, timeZone);
196 a11 = getLunarMonth11(lunarYear, timeZone);
197 b11 = getLunarMonth11(lunarYear+1, timeZone);
199 k = INT(0.5 + (a11 - 2415021.076998695) / 29.530588853);
200 off = lunarMonth - 11;
204 if (b11 - a11 > 365) {
205 leapOff = getLeapMonthOffset(a11, timeZone);
206 leapMonth = leapOff - 2;
210 if (lunarLeap != 0 && lunarMonth != leapMonth) {
211 return new Array(0, 0, 0);
212 } else if (lunarLeap != 0 || off >= leapOff) {
216 monthStart = getNewMoonDay(k+off, timeZone);
217 return jdToDate(monthStart+lunarDay-1);