-- 作者:chwc
-- 发布时间:2009/10/12 18:48:00
-- [原创]最新日九星计算JS代码。(NetGhost:你要的全在里面)
最新日九星计算JS代码。本代码将在“电脑医生万年历 v2.03 绿色免安装版”采用。
代码如下:
//-------通用數學函數或計算函數------// function PI(){///角度 return Math.PI; }
function sin(x){///正弦 return Math.sin(x); }
function cos(x){///餘弦 return Math.cos(x); }
function abs(x){///絕對值 return Math.abs(x); }
function floor(x){///截去小數部份,取整;如:11.25只取出11 return Math.floor(x); }
function round(x){///小數部份舍入n位數;如:11.205 return Math.round(x); }
function tail(x){///取出小數,只保留小數部份;如:11.25只取出0.25 return x-floor(x); } function rem(x,w){ //廣義求余 return tail(x/w)*w; } function nnr(x,w){ //非负余數 if(x<0){ x+=(-x*w); } return x%w; } function ang(x,t,c1,t0,t2,t3){/////角度計算函數 return tail(c1*x)*2*PI()+t0-t2*t*t-t3*t*t*t; } function gzs(v){//干支數 var v1=v;if(v1<0)v1=v+600; var s=(v1+50)%60;if(s==0)s=((v1+50)%60)+60; return s; } function gan(x){//天干數 return x%10; } function zhi(x){//地支數 return x%12; } //--公暦与連續紀日互轉--// //日差天數 function D0(y,m,d){ var ifG=ifGr(y,m,d,1); var monL=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31); if(ifG) if((y%100!=0&&y%4==0)||(y%400==0)) monL[2]+=1; else ; else if(y%4==0) monL[2]+=1; else ; var v=0; for(var i=0;i<=m-1;i++){ v+=monL[i]; } v+=d; if(y==1582){ if(ifG==1) v-=10; if(ifG==-1) v=1/0; //infinity } return v; } //反日差天數 function antiD0(y,x){ var yL=D0(y,12,31)-D0(y,1,0); var yL2=D0(y-1,12,31)-D0(y-1,1,0); if(x<1){ var m=1; var x=x+yL2; for(var j=1;j<=12;j++){ var mL=D0(y-1,j+1,1)-D0(y-1,j,1); if(x<=mL||j==12){ var m=j-12; break; } else x-=mL; } return 100*m+x; }else{ if(x>yL){ var m=1; var x=x-yL; for(var j=1;j<=12;j++){ var mL=D0(y+1,j+1,1)-D0(y+1,j,1); if(x<=mL||j==12){ var m=j+12; break; } else x-=mL; } return 100*m+x; }else{ var m=1; for(var j=1;j<=12;j++){ var mL=D0(y,j+1,1)-D0(y,j,1); if(x<=mL||j==12){ var m=j; break; } else x-=mL; } return 100*m+x; }} } //年差天數 function D(y){ var v=(y-1)*365+floor((y-1)/4); //Julian的年差天數 if(y>1582) v+=-floor((y-1)/100)+floor((y-1)/400); //Gregorian的年差天數 return v; } //等效標準天數 function erD(y,m,d){ var v=(y-1)*365+floor((y-1)/4)+D0(y,m,d)-2; //Julian的等效標準天數 if(y>1582) v+=-floor((y-1)/100)+floor((y-1)/400)+2; //Gregorian的等效標準天數 return v; } //儒略日 function JD(y,m,d,h,min,sec,zone){ var ifG=ifGr(y,m,d,1); var jt=(h+(min+sec/60)/60)/24-0.5-zone/24; var jd=(ifG)?(erD(y,m,d)+1721425+jt):(erD(y,m,d)+1721425+jt);//儒略日 return jd; } function revJD(jd){ //儒略日數轉公曆 var D=\'\';var A=\'\';var D3=0;var D4=0;var D5=0;var D6=365.25; var A=floor(jd+1-0.167);var F=tail(jd);//取得北京時間日數的整數部份A及小數部分F if(A>=2299239)D5=10; if(A>=2299239)D6=365.2425; var y=floor((A+D5)/D6)-4712;var y1=y; var dd9=round(JD(y1,1,1,20,0,0,8)*1000)/1000; var u0D1=A-dd9+1; var sDt1=u0D1; var sD1=antiD0(y,floor(sDt1)); var M=floor(sD1/100); var Dt=sD1%100; var D=Dt; if(y==1582&&M==10&&D>=5)D=Dt+10; var ymd=y+\',\'+M+\',\'+D; return ymd; } //-----農暦節氣計算函數---// var ykche=new Array(///史實平氣擬合1 \'1640650.479938\',\'1642476.703182\',\'1683430.515601\',\'1752157.640664\',\'1807675.003759\',\'1883627.765182\',\'1907369.128100\',\'1936603.140413\',\'1939145.524180\',\'1947180.798300\',\'1964362.041824\',\'1987372.340971\', \'1999653.819126\',\'2007445.469786\',\'2021324.917146\',\'2047257.232342\',\'2070282.898213\',\'2073204.872850\',\'2080144.500926\',\'2086703.688963\',\'2110033.182763\',\'2111190.300888\',\'2113731.271005\',\'2120670.840263\',\'2123973.309063\', \'2125068.997336\',\'2136026.312633\',\'2156099.495538\',\'2159021.324663\',\'2162308.575254\',\'2178485.706538\',\'2178759.662849\',\'2185334.020800\',\'2187525.481425\',\'2188621.191481\')
var kche=new Array(///史實平氣擬合2 \'-221\',\'-216\',\'-104\',\'84\',\'236\',\'444\',\'509\',\'589\',\'596\',\'618\',\'665\',\'728\',\'761\',\'783\',\'821\',\'892\',\'955\',\'963\',\'982\',\'1000\',\'1064\',\'1067\',\'1074\',\'1093\',\'1102\',\'1105\',\'1135\',\'1190\',\'1198\',\'1207\', \'1251\',\'1252\',\'1270\',\'1276\',\'1279\'); var jkche=new Array(////史實平氣擬合3 \'15.21842500\',\'15.21874996\',\' 15.218750011\',\'15.218749978\',\'15.218620279\',\'15.218612292\',\'15.218449176\',\'15.218425000\',\'15.218466998\',\' 15.218524844\',\'15.218533526\',\'15.218513908\',\'15.218530782\', \'15.218535181\',\'15.218526248\',\'15.218519654\',\'15.218425000\',\'15.218515221\',\'15.218530782\',\'15.218523776\',\'15.218425000\',\'15.218425000\',\'15.218515671\',\'15.218425000\',\'15.218425000\',\'15.218477932\',\'15.218472436\', \'15.218425000\',\'15.218425000\',\'15.218461742\',\'15.218425000\',\'15.218445786\',\'15.218425000\',\'15.218425000\',\'15.218437484\'); var jkche2=new Array(///史實平氣擬合4 -3,-3,0,4,4,3,3,4,3,3,4,4,19,3,3,3,4,4,4,3,0,4,3,3,4,4,4,3,3,3,10,4,4,4,4); //// function S(y,n,pd){ //pd取值為0或1,分別表示平氣和定氣,該函數返回節氣的D0值 var yk=365.2423112;var jjk=0.00000001;///yk=365.2422226; var jk=y-2200;var j=(0.000084275/3300)*jk;if(y<=2219)j=0; var jk5=y-5500;var j5=(0.00001232/1860)*jk5;if(y<5500)j5=0; var jk2=y-900;var j2=0.000252155/3600*jk2;if(1001<y)j2=0; var jk3=0.000100121/2012*(y-(-4712));var j3=0.000100121-jk3;if(y>=-2700)j3=0; var yk2=yk-j-j2-j3-j5-jjk; var juD=y*(yk2-6.4e-14*(y-100)*(y-100)-3.047e-8*(y-100))+15.218427*n+1721050.71301;//儒略日 var tht=3e-4*y-0.372781384-0.2617913325*n;//角度 var yrD=(1.945*sin(tht)-0.01206*sin(2*tht))*(1.048994-2.583e-5*y);//年差實均數 var shuoD=-18e-4*sin(2.313908653*y-0.439822951-3.0443*n);//朔差實均 //史實擬合 var jkk=0;if(y>-216||y==-216&&n>=21)jkk=1;if(y>=-103)jkk=2;if(y>85||y==85&&n>=4)jkk=3; if(y>237||y==237&&n>=4)jkk=4;if(y>445||y==445&&n>=3)jkk=5;if(y>510||y==510&&n>=3)jkk=6;if(y>590||y==590&&n>=4)jkk=7; if(y>597||y==597&&n>=3)jkk=8;if(y>619||y==619&&n>=3)jkk=9;if(y>666||y==666&&n>=4)jkk=10;if(y>729||y==729&&n>=4)jkk=11; if(y>762||y==762&&n>=19)jkk=12;if(y>784||y==784&&n>=3)jkk=13;if(y>822||y==822&&n>=3)jkk=14;if(y>893||y==893&&n>=3)jkk=15; if(y>956||y==956&&n>=4)jkk=16;if(y>964||y==964&&n>=4)jkk=17;if(y>983||y==983&&n>=4)jkk=18;if(y>1001||y==1001&&n>=3)jkk=19; if(y>1064||y==1064&&n>=24)jkk=20;if(y>1068||y==1068&&n>=4)jkk=21; if(y>1075||y==1075&&n>=3)jkk=22;if(y>1094||y==1094&&n>=3)jkk=23;if(y>1103||y==1103&&n>=4)jkk=24; if(y>1106||y==1106&&n>=4)jkk=25;if(y>1136||y==1136&&n>=4)jkk=26;if(y>1191||y==1191&&n>=3)jkk=27;if(y>1199||y==1199&&n>=3)jkk=28; if(y>1208||y==1208&&n>=3)jkk=29;if(y>1252||y==1252&&n>=10)jkk=30;if(y>1253||y==1253&&n>=4)jkk=31;if(y>1271||y==1271&&n>=4)jkk=32; if(y>1277||y==1277&&n>=4)jkk=33;if(y>1280||y==1280&&n>=4)jkk=34; var yk2=ykche[jkk];///初數 var k=kche[jkk];//年界 var jk=jkche[jkk];////氣長 var jk9=jkche2[jkk];///節差數 //史實計算 var ykd=(round(JD(y,1,1,20,0,0,8)*1000)/1000)-1; var yk3=jk*(jk9*1); var yk=(yk2*1)-(yk3*1); var jq=jk*(n+(24*(y-k-1))); var vj=jq+yk-ykd+0.5;if(y<-220||y>1646||y==1646&&n>=3)vj=\'\'; var vs=(pd)?(juD+yrD+shuoD-erD(y,1,0)-1721425):vj;//vj為古代平氣,現代定氣‘(juD+yrD+shuoD-erD(y,1,0)-1721425)’,現代平氣:‘(juD-erD(y,1,0)-1721425)’ return vs; } ////九星計算開始 var jxName = new Array(\'九紫\',\'八白\',\'七赤\',\'六白\',\'五黃\',\'四緑\',\'三碧\',\'二黒\',\'一白\'); var jxName2= new Array(\'--天乙星(火)--吉神\',\'--太陰星(土)--吉神\',\'--鹹池星(金)--凶神\',\'--青龍星(金)--吉神\',\'--天符星(土)--凶神\',\'--招搖星(木)--安神\',\'--軒轅星(木)--安神\',\'--攝提星(土)--凶神\',\'--太乙星(水)--吉神\'); function jxd(y,m,d){/////////九星計算函數 var jxt;var jx0,jx1,jx2,jx3,jx4; var d0s=floor((round(JD(y,1,1,1,0,0,8)*1000)/1000)-0.167); var ds=floor((round(JD(y,m,d,1,0,0,8)*1000)/1000)-0.167);//當前儒略日 var s_12=floor(S(y-1,12,1));//上年夏至 var s0=floor(S(y,0,1));//上年冬至 var s12=floor(S(y,12,1));//當年夏至 var s24=floor(S(y,24,1));//當年冬至 var s12a=floor(S(y+1,12,1));//下年夏至 var jxxz1=floor((round(JD(y-1,1,1,1,0,0,8)*1000)/1000)-0.167)+s_12;//求上年夏至儒略日 var jk1=gzs(jxxz1);var xzjz1=60-jk1;//上年夏至後甲子反差天 if(jk1<30)xzjz1=-jk1; var jz1=(jxxz1+xzjz1);//求上年夏至後甲子儒略日 jx0=ds-jz1; var jxdz1=floor(d0s+s0);//求上年冬至儒略日 var jk2=gzs(jxdz1);var dzjz1=60-jk2;//上年冬至後甲子反差天 if(jk2<30)dzjz1=-jk2; var jz2=(jxdz1+dzjz1);//上年冬至後甲子儒略日 jx1=ds-jz2; var jxxz2=floor(d0s+s12);//求當年夏至儒略日 var jk3=gzs(jxxz2);var xzjz2=60-jk3;//當年夏至後甲子反差天 if(jk3<30)xzjz2=-jk3; var jz3=(jxxz2+xzjz2);//當年夏至後甲子儒略日 jx2=ds-jz3; var jxdz2=floor(d0s+s24);//求當年冬至儒略日 var jk4=gzs(jxdz2);var dzjz2=60-jk4;//當年冬至後甲子的反差天 if(jk4<30)dzjz2=-jk4; var jz4=(jxdz2+dzjz2);//當年冬至後甲子儒略日 jx3=ds-jz4; var jxxz3=floor(((round(JD(y+1,1,1,1,0,0,8)*1000)/1000)-0.167)+s12a);//求上年夏至儒略日 var jk5=gzs(jxxz3);var xzjz3=60-jk5;//下年夏至後甲子的反差天 if(jk5<30)xzjz3=-jk5; var jz5=(jxxz3+xzjz3);//下年夏至後甲子儒略日 jx4=ds-jz5; var jx1k=(jz2-jz1-180);var j1k=0;var j1ka=0;var jx2k=(jz3-jz2-180);var j2k=0;var j2ka=0; var jx3k=(jz4-jz3-180);var j3k=0;var j3ka=0;var jx4k=(jz5-jz4-180);var j4k=0;var j4ka=0; if(jx1k>=60)j1k=30; if(jx1k==-60)j1k=-33;if(jx1k==-60)j1ka=6;
if(jx2k>=60&&jx1k==0)j2k=30; if(jx2k==-60)j2k=-33;if(jx2k==-60||jx1k==-60)j2ka=9;
if(jx3k>=60&&jx2k==0)j3k=30; if(jx3k==-60)j3k=-33;if(jx3k==-60||jx2k==-60)j3ka=6;
if(jx4k>=60&&jx3k==0)j4k=30; if(jx4k==-60)j4k=-33;if(jx4k==-60||jx3k==-60)j4ka=9;
if(ds>=jz1)jxt=(jx0%9);if(ds>=jz2-j1k)jxt=(9-((jx1-j1ka)%9+1))%9;if(ds>=jz3-j2k)jxt=((jx2-j2ka)%9);if(ds>=jz4-j3k)jxt=(9-((jx3-j3ka)%9+1))%9; if(ds>=jz5-j4k)jxt=((jx4-j4ka)%9);if(jxt<0&&y>=-4711)jxt=(jxt+9)%9; return \'日九星:\'+(jxName[jxt]+jxName2[jxt]); }
調用==jxd(y,m,d)
|