最近对星座很感兴趣,想做一个查询星座的程序,算法是现有的,还有就是阴历和阳历转换的问题,所以有找的阴历和阳历相互转换的程序,不过是js,来自搜狐星座的js代码,经过修改加工,放到WM上面。代码和程序放到最后贴上。
希望能够认识喜欢手机开发的朋友,一起交流学习。
阴历和阳历相互转换js代码如下
js代码
1 <script language="JavaScript"><!--
2 function CalConv(M)
3 {
4
5 FIRSTYEAR = 1936;
6 LASTYEAR = 2031;
7
8 LunarCal = [
9 new tagLunarCal(23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ), /* 1936 */
10 new tagLunarCal( 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ),
11 new tagLunarCal( 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ),
12 new tagLunarCal( 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
13 new tagLunarCal( 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 1940 */
14 new tagLunarCal( 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
15 new tagLunarCal( 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
16 new tagLunarCal( 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
17 new tagLunarCal( 24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1944 */
18 new tagLunarCal( 43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
19 new tagLunarCal( 32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ),
20 new tagLunarCal( 21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
21 new tagLunarCal( 40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ), /* 1948 */
22 new tagLunarCal( 28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
23 new tagLunarCal( 47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1 ),
24 new tagLunarCal( 36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
25 new tagLunarCal( 26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ), /* 1952 */
26 new tagLunarCal( 44, 0, 3, 47, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1 ),
27 new tagLunarCal( 33, 0, 4, 52, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ),
28 new tagLunarCal( 23, 3, 5, 57, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
29 new tagLunarCal( 42, 0, 6, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ), /* 1956 */
30 new tagLunarCal( 30, 8, 1, 8, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
31 new tagLunarCal( 48, 0, 2, 13, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
32 new tagLunarCal( 38, 0, 3, 18, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
33 new tagLunarCal( 27, 6, 4, 23, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 1960 */
34 new tagLunarCal( 45, 0, 6, 29, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ),
35 new tagLunarCal( 35, 0, 0, 34, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ),
36 new tagLunarCal( 24, 4, 1, 39, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
37 new tagLunarCal( 43, 0, 2, 44, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ), /* 1964 */
38 new tagLunarCal( 32, 0, 4, 50, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
39 new tagLunarCal( 20, 3, 5, 55, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
40 new tagLunarCal( 39, 0, 6, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
41 new tagLunarCal( 29, 7, 0, 5, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 1968 */
42 new tagLunarCal( 47, 0, 2, 11, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
43 new tagLunarCal( 36, 0, 3, 16, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ),
44 new tagLunarCal( 26, 5, 4, 21, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 ),
45 new tagLunarCal( 45, 0, 5, 26, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1972 */
46 new tagLunarCal( 33, 0, 0, 32, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1 ),
47 new tagLunarCal( 22, 4, 1, 37, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
48 new tagLunarCal( 41, 0, 2, 42, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ),
49 new tagLunarCal( 30, 8, 3, 47, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ), /* 1976 */
50 new tagLunarCal( 48, 0, 5, 53, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 ),
51 new tagLunarCal( 37, 0, 6, 58, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
52 new tagLunarCal( 27, 6, 0, 3, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ),
53 new tagLunarCal( 46, 0, 1, 8, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 1980 */
54 new tagLunarCal( 35, 0, 3, 14, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 ),
55 new tagLunarCal( 24, 4, 4, 19, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ),
56 new tagLunarCal( 43, 0, 5, 24, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ),
57 new tagLunarCal( 32, 10, 6, 29, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ), /* 1984 */
58 new tagLunarCal( 50, 0, 1, 35, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
59 new tagLunarCal( 39, 0, 2, 40, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1 ),
60 new tagLunarCal( 28, 6, 3, 45, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 ),
61 new tagLunarCal( 47, 0, 4, 50, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 1988 */
62 new tagLunarCal( 36, 0, 6, 56, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ),
63 new tagLunarCal( 26, 5, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1 ),
64 new tagLunarCal( 45, 0, 1, 6, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0 ),
65 new tagLunarCal( 34, 0, 2, 11, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0 ), /* 1992 */
66 new tagLunarCal( 22, 3, 4, 17, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
67 new tagLunarCal( 40, 0, 5, 22, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
68 new tagLunarCal( 30, 8, 6, 27, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1 ),
69 new tagLunarCal( 49, 0, 0, 32, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1 ), /* 1996 */
70 new tagLunarCal( 37, 0, 2, 38, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ),
71 new tagLunarCal( 27, 5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 ),
72 new tagLunarCal( 46, 0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ), /* 1999 */
73 new tagLunarCal( 35, 0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ), /* 2000 */
74 new tagLunarCal( 23, 4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
75 new tagLunarCal( 42, 0, 1, 4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
76 new tagLunarCal( 31, 0, 2, 9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
77 new tagLunarCal( 21, 2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 2004 */
78 new tagLunarCal( 39, 0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
79 new tagLunarCal( 28, 7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ),
80 new tagLunarCal( 48, 0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
81 new tagLunarCal( 37, 0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 2008 */
82 new tagLunarCal( 25, 5, 3, 41, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
83 new tagLunarCal( 44, 0, 4, 46, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
84 new tagLunarCal( 33, 0, 5, 51, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
85 new tagLunarCal( 22, 4, 6, 56, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 2012 */
86 new tagLunarCal( 40, 0, 1, 2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
87 new tagLunarCal( 30, 9, 2, 7, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
88 new tagLunarCal( 49, 0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 ),
89 new tagLunarCal( 38, 0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 2016 */
90 new tagLunarCal( 27, 6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
91 new tagLunarCal( 46, 0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 ),
92 new tagLunarCal( 35, 0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
93 new tagLunarCal( 24, 4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 2020 */
94 new tagLunarCal( 42, 0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
95 new tagLunarCal( 31, 0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ),
96 new tagLunarCal( 21, 2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
97 new tagLunarCal( 40, 0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 2024 */
98 new tagLunarCal( 28, 6, 2, 5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
99 new tagLunarCal( 47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 ),
100 new tagLunarCal( 36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
101 new tagLunarCal( 25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ), /* 2028 */
102 new tagLunarCal( 43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ),
103 new tagLunarCal( 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 ),
104 new tagLunarCal( 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ) ];
105
106
107 /* 西曆年每月之日數 */
108 SolarCal = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
109
110 /* 西曆年每月之累積日數, 平年與閏年 */
111 SolarDays = [
112 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396,
113 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 ];
114
115 AnimalIdx = ["馬 ", "羊 ", "猴 ", "雞 ", "狗 ", "豬 ", "鼠 ", "牛 ", "虎 ", "兔 ", "龍 ", "蛇 " ];
116 LocationIdx = [ "南", "東", "北", "西" ];
117
118
119 if (M==0) { //阳历到阴历
120 if (!IsInteger(form_jisuan.yyear.value) || !IsInteger(form_jisuan.ymonth.value) || !IsInteger(form_jisuan.yday.value)) return alert("请输入合法阳历年月日数值");
121 SolarYear = parseInt(form_jisuan.yyear.value);
122 SolarMonth = parseInt(form_jisuan.ymonth.value);
123 SolarDate = parseInt(form_jisuan.yday.value);
124
125
126 if ( SolarYear <= FIRSTYEAR || SolarYear > LASTYEAR ) return alert("请输入1936-2031有效年份");
127
128 sm = SolarMonth - 1;
129
130 if ( sm < 0 || sm > 11 ) return alert(请输入有效月份);
131
132 leap = GetLeap( SolarYear );
133
134 if ( sm == 1 )
135 d = leap + 28;
136 else
137 d = SolarCal[sm];
138
139 if ( SolarDate < 1 || SolarDate > d ) return 3;
140
141 y = SolarYear - FIRSTYEAR;
142 acc = SolarDays[ leap*14 + sm ] + SolarDate;
143 kc = acc + LunarCal[y].BaseKanChih;
144 Kan = kc % 10;
145 Chih = kc % 12;
146 Location = LocationIdx[kc % 4];
147 Age = kc % 60;
148 if ( Age < 22 )
149 Age = 22 - Age;
150 else
151 Age = 82 - Age;
152
153 Age =Age + 3;
154
155 if (Age < 10)
156 Age=Age+60;
157
158 Animal = AnimalIdx[ Chih ];
159
160 if ( acc <= LunarCal[y].BaseDays ) {
161 y--;
162 LunarYear = SolarYear - 1;
163 leap = GetLeap( LunarYear );
164 sm += 12;
165 acc = SolarDays[leap*14 + sm] + SolarDate;
166 }
167 else
168 LunarYear = SolarYear;
169
170 l1 = LunarCal[y].BaseDays;
171 for ( i=0; i<13; i++ ) {
172 l2 = l1 + LunarCal[y].MonthDays[i] + 29;
173 if ( acc <= l2 ) break;
174 l1 = l2;
175 }
176
177 LunarMonth = i + 1;
178 LunarDate = acc - l1;
179 im = LunarCal[y].Intercalation;
180
181 if ( im != 0 && LunarMonth > im ) {
182 LunarMonth--;
183 if ( LunarMonth == im ) LunarMonth = -im;
184 }
185
186 if ( LunarMonth > 12 ) LunarMonth -= 12;
187
188 alert("农历为:"+ LunarYear + "年" + LunarMonth + "月 " + LunarDate + "日 " );
189
190 form_jisuan.yyear.value = "";
191 form_jisuan.ymonth.value = "";
192 form_jisuan.yday.value = "";
193 return 0;
194 }
195
196 else /* 阴历转阳历 */
197 {
198 if (!IsInteger(form_jisuan.nyear.value) || !IsInteger(form_jisuan.nmonth.value) || !IsInteger(form_jisuan.nday.value)) return alert("请输入合法农历年月日数值");
199 LunarYear = parseInt(form_jisuan.nyear.value);
200 LunarMonth = parseInt(form_jisuan.nmonth.value);
201 LunarDate = parseInt(form_jisuan.nday.value);
202
203 if ( LunarYear < FIRSTYEAR || LunarYear >= LASTYEAR ) return alert("请输入1936-2031有效年份");
204
205 y = LunarYear - FIRSTYEAR ;
206 im = LunarCal[y].Intercalation;
207 lm = LunarMonth;
208
209 if ( lm < 0 )
210 {
211 if ( lm != -im )
212 return alert(请输入有效月份);
213 }
214 else if ( lm < 1 || lm > 12 ) return alert(请输入有效月份);
215
216 if ( im != 0 )
217 {
218 if ( lm > im )
219 lm++;
220 else if ( lm == -im )
221 lm = im + 1;
222 }
223 lm--;
224 if ( LunarDate > LunarCal[y].MonthDays[lm] + 29 )
225 return alert("农历日期不正确");
226 acc = 0;
227 for ( i=0; i < lm;i++) {
228 acc+= LunarCal[y].MonthDays[i] + 29;
229 }
230 acc +=LunarCal[y].BaseDays + LunarDate;
231 leap = GetLeap( LunarYear );
232 for ( i=13; i>=0; i-- ) {
233 if ( acc > SolarDays[leap*14+i] )
234 break;
235 }
236 SolarDate = acc - SolarDays[leap*14 + i] ;
237 if ( i <= 11 )
238 {
239 SolarYear = LunarYear;
240 SolarMonth = i + 1;
241 }
242 else
243 {
244
245 SolarYear = LunarYear + 1;
246 SolarMonth = i - 11;
247 }
248 leap = GetLeap( SolarYear );
249 y = SolarYear - FIRSTYEAR;
250 //acc = SolarDays[leap][SolarMonth-1] + SolarDate;
251 acc = SolarDays[leap*14 + SolarMonth-1] + SolarDate;
252 weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
253 kc = acc + LunarCal[y].BaseKanChih;
254 kan = kc % 10;
255 chih = kc % 12;
256
257 alert("阳历为:"+ SolarYear + "年" + SolarMonth + "月" + SolarDate + "日 " );
258 form_jisuan.nyear.value = "";
259 form_jisuan.nmonth.value = "";
260 form_jisuan.nday.value = "";
261 return 0;
262 }//else结束
263
264 }
265
266 /* 闰年, 返回 0 平年, 1 闰年 */
267 function GetLeap( year )
268 {
269 if ( year % 400 == 0 )
270 return 1;
271 else if ( year % 100 == 0 )
272 return 0;
273 else if ( year % 4 == 0 )
274 return 1;
275 else
276 return 0;
277 }
278
279 function tagLunarCal( d, i, w, k, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13) {
280 this.BaseDays = d; /* 1 月 1 日到正月初一的累计日 */
281 this.Intercalation = i; /* 闰月月份. 0==此年沒有闰月 */
282 this.BaseWeekday = w; /* 此年 1 月 1 日为星期减 1 */
283 this.BaseKanChih = k; /* 此年 1 月 1 日之干支序号减 1 */
284 this.MonthDays = [ m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13 ]; /* 此农历年每月之大小, 0==小月(29日), 1==大月(30日) */
285 }
286 //--></script>
2 function CalConv(M)
3 {
4
5 FIRSTYEAR = 1936;
6 LASTYEAR = 2031;
7
8 LunarCal = [
9 new tagLunarCal(23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ), /* 1936 */
10 new tagLunarCal( 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ),
11 new tagLunarCal( 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ),
12 new tagLunarCal( 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
13 new tagLunarCal( 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 1940 */
14 new tagLunarCal( 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
15 new tagLunarCal( 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
16 new tagLunarCal( 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
17 new tagLunarCal( 24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1944 */
18 new tagLunarCal( 43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
19 new tagLunarCal( 32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ),
20 new tagLunarCal( 21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
21 new tagLunarCal( 40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ), /* 1948 */
22 new tagLunarCal( 28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
23 new tagLunarCal( 47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1 ),
24 new tagLunarCal( 36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
25 new tagLunarCal( 26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ), /* 1952 */
26 new tagLunarCal( 44, 0, 3, 47, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1 ),
27 new tagLunarCal( 33, 0, 4, 52, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ),
28 new tagLunarCal( 23, 3, 5, 57, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
29 new tagLunarCal( 42, 0, 6, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ), /* 1956 */
30 new tagLunarCal( 30, 8, 1, 8, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
31 new tagLunarCal( 48, 0, 2, 13, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
32 new tagLunarCal( 38, 0, 3, 18, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
33 new tagLunarCal( 27, 6, 4, 23, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 1960 */
34 new tagLunarCal( 45, 0, 6, 29, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ),
35 new tagLunarCal( 35, 0, 0, 34, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ),
36 new tagLunarCal( 24, 4, 1, 39, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
37 new tagLunarCal( 43, 0, 2, 44, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ), /* 1964 */
38 new tagLunarCal( 32, 0, 4, 50, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
39 new tagLunarCal( 20, 3, 5, 55, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
40 new tagLunarCal( 39, 0, 6, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
41 new tagLunarCal( 29, 7, 0, 5, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 1968 */
42 new tagLunarCal( 47, 0, 2, 11, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
43 new tagLunarCal( 36, 0, 3, 16, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ),
44 new tagLunarCal( 26, 5, 4, 21, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 ),
45 new tagLunarCal( 45, 0, 5, 26, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1972 */
46 new tagLunarCal( 33, 0, 0, 32, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1 ),
47 new tagLunarCal( 22, 4, 1, 37, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
48 new tagLunarCal( 41, 0, 2, 42, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ),
49 new tagLunarCal( 30, 8, 3, 47, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ), /* 1976 */
50 new tagLunarCal( 48, 0, 5, 53, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 ),
51 new tagLunarCal( 37, 0, 6, 58, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
52 new tagLunarCal( 27, 6, 0, 3, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ),
53 new tagLunarCal( 46, 0, 1, 8, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 1980 */
54 new tagLunarCal( 35, 0, 3, 14, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 ),
55 new tagLunarCal( 24, 4, 4, 19, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ),
56 new tagLunarCal( 43, 0, 5, 24, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ),
57 new tagLunarCal( 32, 10, 6, 29, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ), /* 1984 */
58 new tagLunarCal( 50, 0, 1, 35, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
59 new tagLunarCal( 39, 0, 2, 40, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1 ),
60 new tagLunarCal( 28, 6, 3, 45, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 ),
61 new tagLunarCal( 47, 0, 4, 50, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 1988 */
62 new tagLunarCal( 36, 0, 6, 56, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ),
63 new tagLunarCal( 26, 5, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1 ),
64 new tagLunarCal( 45, 0, 1, 6, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0 ),
65 new tagLunarCal( 34, 0, 2, 11, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0 ), /* 1992 */
66 new tagLunarCal( 22, 3, 4, 17, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
67 new tagLunarCal( 40, 0, 5, 22, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
68 new tagLunarCal( 30, 8, 6, 27, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1 ),
69 new tagLunarCal( 49, 0, 0, 32, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1 ), /* 1996 */
70 new tagLunarCal( 37, 0, 2, 38, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ),
71 new tagLunarCal( 27, 5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 ),
72 new tagLunarCal( 46, 0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ), /* 1999 */
73 new tagLunarCal( 35, 0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ), /* 2000 */
74 new tagLunarCal( 23, 4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
75 new tagLunarCal( 42, 0, 1, 4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
76 new tagLunarCal( 31, 0, 2, 9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
77 new tagLunarCal( 21, 2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 2004 */
78 new tagLunarCal( 39, 0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
79 new tagLunarCal( 28, 7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ),
80 new tagLunarCal( 48, 0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
81 new tagLunarCal( 37, 0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 2008 */
82 new tagLunarCal( 25, 5, 3, 41, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
83 new tagLunarCal( 44, 0, 4, 46, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
84 new tagLunarCal( 33, 0, 5, 51, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
85 new tagLunarCal( 22, 4, 6, 56, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 2012 */
86 new tagLunarCal( 40, 0, 1, 2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
87 new tagLunarCal( 30, 9, 2, 7, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
88 new tagLunarCal( 49, 0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 ),
89 new tagLunarCal( 38, 0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 2016 */
90 new tagLunarCal( 27, 6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
91 new tagLunarCal( 46, 0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 ),
92 new tagLunarCal( 35, 0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
93 new tagLunarCal( 24, 4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 2020 */
94 new tagLunarCal( 42, 0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
95 new tagLunarCal( 31, 0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ),
96 new tagLunarCal( 21, 2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
97 new tagLunarCal( 40, 0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 2024 */
98 new tagLunarCal( 28, 6, 2, 5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
99 new tagLunarCal( 47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 ),
100 new tagLunarCal( 36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
101 new tagLunarCal( 25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ), /* 2028 */
102 new tagLunarCal( 43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ),
103 new tagLunarCal( 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 ),
104 new tagLunarCal( 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ) ];
105
106
107 /* 西曆年每月之日數 */
108 SolarCal = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
109
110 /* 西曆年每月之累積日數, 平年與閏年 */
111 SolarDays = [
112 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396,
113 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 ];
114
115 AnimalIdx = ["馬 ", "羊 ", "猴 ", "雞 ", "狗 ", "豬 ", "鼠 ", "牛 ", "虎 ", "兔 ", "龍 ", "蛇 " ];
116 LocationIdx = [ "南", "東", "北", "西" ];
117
118
119 if (M==0) { //阳历到阴历
120 if (!IsInteger(form_jisuan.yyear.value) || !IsInteger(form_jisuan.ymonth.value) || !IsInteger(form_jisuan.yday.value)) return alert("请输入合法阳历年月日数值");
121 SolarYear = parseInt(form_jisuan.yyear.value);
122 SolarMonth = parseInt(form_jisuan.ymonth.value);
123 SolarDate = parseInt(form_jisuan.yday.value);
124
125
126 if ( SolarYear <= FIRSTYEAR || SolarYear > LASTYEAR ) return alert("请输入1936-2031有效年份");
127
128 sm = SolarMonth - 1;
129
130 if ( sm < 0 || sm > 11 ) return alert(请输入有效月份);
131
132 leap = GetLeap( SolarYear );
133
134 if ( sm == 1 )
135 d = leap + 28;
136 else
137 d = SolarCal[sm];
138
139 if ( SolarDate < 1 || SolarDate > d ) return 3;
140
141 y = SolarYear - FIRSTYEAR;
142 acc = SolarDays[ leap*14 + sm ] + SolarDate;
143 kc = acc + LunarCal[y].BaseKanChih;
144 Kan = kc % 10;
145 Chih = kc % 12;
146 Location = LocationIdx[kc % 4];
147 Age = kc % 60;
148 if ( Age < 22 )
149 Age = 22 - Age;
150 else
151 Age = 82 - Age;
152
153 Age =Age + 3;
154
155 if (Age < 10)
156 Age=Age+60;
157
158 Animal = AnimalIdx[ Chih ];
159
160 if ( acc <= LunarCal[y].BaseDays ) {
161 y--;
162 LunarYear = SolarYear - 1;
163 leap = GetLeap( LunarYear );
164 sm += 12;
165 acc = SolarDays[leap*14 + sm] + SolarDate;
166 }
167 else
168 LunarYear = SolarYear;
169
170 l1 = LunarCal[y].BaseDays;
171 for ( i=0; i<13; i++ ) {
172 l2 = l1 + LunarCal[y].MonthDays[i] + 29;
173 if ( acc <= l2 ) break;
174 l1 = l2;
175 }
176
177 LunarMonth = i + 1;
178 LunarDate = acc - l1;
179 im = LunarCal[y].Intercalation;
180
181 if ( im != 0 && LunarMonth > im ) {
182 LunarMonth--;
183 if ( LunarMonth == im ) LunarMonth = -im;
184 }
185
186 if ( LunarMonth > 12 ) LunarMonth -= 12;
187
188 alert("农历为:"+ LunarYear + "年" + LunarMonth + "月 " + LunarDate + "日 " );
189
190 form_jisuan.yyear.value = "";
191 form_jisuan.ymonth.value = "";
192 form_jisuan.yday.value = "";
193 return 0;
194 }
195
196 else /* 阴历转阳历 */
197 {
198 if (!IsInteger(form_jisuan.nyear.value) || !IsInteger(form_jisuan.nmonth.value) || !IsInteger(form_jisuan.nday.value)) return alert("请输入合法农历年月日数值");
199 LunarYear = parseInt(form_jisuan.nyear.value);
200 LunarMonth = parseInt(form_jisuan.nmonth.value);
201 LunarDate = parseInt(form_jisuan.nday.value);
202
203 if ( LunarYear < FIRSTYEAR || LunarYear >= LASTYEAR ) return alert("请输入1936-2031有效年份");
204
205 y = LunarYear - FIRSTYEAR ;
206 im = LunarCal[y].Intercalation;
207 lm = LunarMonth;
208
209 if ( lm < 0 )
210 {
211 if ( lm != -im )
212 return alert(请输入有效月份);
213 }
214 else if ( lm < 1 || lm > 12 ) return alert(请输入有效月份);
215
216 if ( im != 0 )
217 {
218 if ( lm > im )
219 lm++;
220 else if ( lm == -im )
221 lm = im + 1;
222 }
223 lm--;
224 if ( LunarDate > LunarCal[y].MonthDays[lm] + 29 )
225 return alert("农历日期不正确");
226 acc = 0;
227 for ( i=0; i < lm;i++) {
228 acc+= LunarCal[y].MonthDays[i] + 29;
229 }
230 acc +=LunarCal[y].BaseDays + LunarDate;
231 leap = GetLeap( LunarYear );
232 for ( i=13; i>=0; i-- ) {
233 if ( acc > SolarDays[leap*14+i] )
234 break;
235 }
236 SolarDate = acc - SolarDays[leap*14 + i] ;
237 if ( i <= 11 )
238 {
239 SolarYear = LunarYear;
240 SolarMonth = i + 1;
241 }
242 else
243 {
244
245 SolarYear = LunarYear + 1;
246 SolarMonth = i - 11;
247 }
248 leap = GetLeap( SolarYear );
249 y = SolarYear - FIRSTYEAR;
250 //acc = SolarDays[leap][SolarMonth-1] + SolarDate;
251 acc = SolarDays[leap*14 + SolarMonth-1] + SolarDate;
252 weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
253 kc = acc + LunarCal[y].BaseKanChih;
254 kan = kc % 10;
255 chih = kc % 12;
256
257 alert("阳历为:"+ SolarYear + "年" + SolarMonth + "月" + SolarDate + "日 " );
258 form_jisuan.nyear.value = "";
259 form_jisuan.nmonth.value = "";
260 form_jisuan.nday.value = "";
261 return 0;
262 }//else结束
263
264 }
265
266 /* 闰年, 返回 0 平年, 1 闰年 */
267 function GetLeap( year )
268 {
269 if ( year % 400 == 0 )
270 return 1;
271 else if ( year % 100 == 0 )
272 return 0;
273 else if ( year % 4 == 0 )
274 return 1;
275 else
276 return 0;
277 }
278
279 function tagLunarCal( d, i, w, k, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13) {
280 this.BaseDays = d; /* 1 月 1 日到正月初一的累计日 */
281 this.Intercalation = i; /* 闰月月份. 0==此年沒有闰月 */
282 this.BaseWeekday = w; /* 此年 1 月 1 日为星期减 1 */
283 this.BaseKanChih = k; /* 此年 1 月 1 日之干支序号减 1 */
284 this.MonthDays = [ m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13 ]; /* 此农历年每月之大小, 0==小月(29日), 1==大月(30日) */
285 }
286 //--></script>
源程序下载地址: