├── .gitignore ├── img ├── star.png ├── star-ico.png └── star_empty.png ├── index.html ├── README.md └── js ├── ziwei.css ├── ziweistar.js ├── ziweicore.js ├── ziweiui.js └── lunar.js /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /img/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cubshuang/ZiWeiDouShu/HEAD/img/star.png -------------------------------------------------------------------------------- /img/star-ico.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cubshuang/ZiWeiDouShu/HEAD/img/star-ico.png -------------------------------------------------------------------------------- /img/star_empty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cubshuang/ZiWeiDouShu/HEAD/img/star_empty.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 紫微斗數 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZiweiDouShu 2 | 3 | **ZiweiDouShu** 是一個以Html+JavaScript+CSS寫成的簡易紫微斗數命盤產生網頁工具 4 | >**ZiweiDouShu** is a simple web page for generate ZiWeiDouShu Chart 5 | by using Html+JavaScript+CSS. 6 | 7 | ## 命盤內容 8 | #### 【命身十二宫】 9 | * 命宮、父母宮、福德宮、田宅宮、官祿宮、交友宮、遷移宮、疾厄宮、財帛宮、子女宮、夫妻宮、兄弟宮、身宮 10 | #### 【各級星曜】 11 | **主星** 12 | * 紫微、天機、太陽、武曲、天同、廉貞、天府、太陰、貪狼、巨門、天相、天梁、七殺、破軍 13 | 14 | **輔星** 15 | * 六吉星:文昌、文曲、左輔、右弼、天魁、天鉞、祿存 16 | * 四化星:化祿、化權、化科、化忌 17 | * 六煞星:擎羊、陀羅、火星、鈴星、天空、地劫 18 | 19 | **雜曜** 20 | * 天馬、龍池、鳳閣、紅鸞、天喜 21 | >[目前版本僅列示以上星曜] 22 | 23 | #### 【大小限】 24 | * 大限:10年行運 25 | * 小限:1年行運 26 | 27 | ## Demo 28 | * [ZiweiDouShu 紫微斗數](https://cubshuang.github.io/ZiWeiDouShu/) 29 | 30 | ## Reference 31 | * Wiki [紫微斗數](https://zh.wikipedia.org/wiki/%E7%B4%AB%E5%BE%AE%E6%96%97%E6%95%B0) 32 | 33 | ## 版本 34 | * v1:原始版本 35 | * v2:將生成html與計算命盤的部分分開 36 | * v3.1: 1.使用另一個lunar.js,增加可計算的年份 2.將ziwei.js分拆為ziweicore.js及ziweicoreui.js 37 | * v4: 使用原生js函數,取代jquery 38 | * v5: 使用Gird排版 39 | * 目前版本:v5 [master] 40 | 41 | -------------------------------------------------------------------------------- /js/ziwei.css: -------------------------------------------------------------------------------- 1 | body { padding: 10px 25px;margin: 0;background-image:url('../img/star.png');background-repeat:repeat;} 2 | .ziwei{ display: grid;border:4px darkgray groove; padding:1px; height:90%; 3 | grid-template-columns: repeat(4,25%); 4 | grid-template-rows: repeat(4,25%);} 5 | .ziwei,.zwDivHeader{ width:60%; } 6 | @media only screen and (max-width:1024px){ 7 | .ziwei,.zwDivHeader{ width:100%; } body{padding-bottom: 20px;}} 8 | .zwDivCenter{ grid-column-start: 2; grid-column-end: 4; grid-row-start: 2; grid-row-end: 4; } 9 | .ziwei>div { border:1px solid #B7B7B7;margin:1px;position:relative;font-size:1em;} 10 | .zwDivHeader,.zwDivHeader h2{ margin:10px 0; position:relative;} 11 | .zwDivHeader>div{top:0;right:0;} 12 | .zwDivHeader div { margin-bottom:5px; padding:5px; background-color:#CCFFCC;} 13 | .zwDivHeader input[type=button],select,input[type=radio]:first-of-type{margin-left:15px;} 14 | .zwDivCenter{position:relative;border:1px solid #B7B7B7;margin:1px;font-weight: bold;} 15 | .zwDivCenter div{text-align:left;margin:10px;} 16 | .zwDivCenter .zwcopy{bottom:0;right:0;font-size:1em;font-weight: normal;} 17 | .StarA,.StarB,.StarC,.MangA,.MangB,.MangC,.MangY10,.MangY1,.zwcopy,.zwDivHeader>div{position:absolute;} 18 | .starDiv{border:0px solid blue;margin:5px;} 19 | .StarA{top:0;color:blue;} 20 | .StarA span{color:Fuchsia;} 21 | .Star4 {margin-top:8px;} 22 | .Star4 span{color:#8000FF;font-weight:normal;} 23 | .StarB{color:Red;top:0;} 24 | .StarC{text-align: right;top:75px;right:0;width:100%;} 25 | .StarA,.StarC{text-align: right;} 26 | .StarAll{display:none;} 27 | .MangB{width:100%;font-weight:bold;} 28 | .MangC{color:blue;} 29 | .MangA,.MangB,.MangC{bottom:0;} 30 | .MangY10,.MangY1{width:100%; height:1.5em;font-size:0.8em;} 31 | .MangY10,.MangY1,.MangB{text-align: center;} 32 | .MangY10{bottom:3.2em;color:purple;} 33 | .MangY1{bottom:1.8em;color:navy;} 34 | .zwStarRight,.MangC{right:0;} 35 | .zwStarLeft,.MangA{left:0;} -------------------------------------------------------------------------------- /js/ziweistar.js: -------------------------------------------------------------------------------- 1 | /*紫微斗數 Chinese Astrology Zi Wei Dou Shu*/ 2 | /*天干地支五行局*/ 3 | var YinYang=["陽","陰"]; 4 | var HeavenlyStems=["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"]; 5 | var ShengXiaoGB=["鼠", "牛", "虎", "兔", "龍", "蛇","馬", "羊", "猴", "雞", "狗", "豬"]; 6 | var EarthlyBranches=["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"]; 7 | var Palace=["【命宮】","【父母宮】","【福德宮】","【田宅宮】","【官祿宮】","【交友宮】","【遷移宮】","【疾厄宮】","【財帛宮】","【子女宮】","【夫妻宮】","【兄弟宮】","【身】"]; 8 | var FiveElements=["水二局","火六局","土五局","木三局","金四局"]; 9 | var DaShian=[2,6,5,3,4];//搭配五行局 10 | /*Stars*/ 11 | var StarM_A14=["紫微","天機","太陽","武曲","天同","廉貞","天府","太陰","貪狼","巨門","天相","天梁","七殺","破軍"]; 12 | var StarM_A07=["文昌","文曲","左輔","右弼","天魁","天鉞","祿存"]; 13 | var StarM_S04=["化祿","化權","化科","化忌"]; 14 | var StarM_B06=["擎羊","陀羅","火星","鈴星","天空","地劫"]; 15 | var StarO_S05=["天馬","龍池","鳳閣","紅鸞","天喜"]; 16 | /*0:水二局:[1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,0,0,1,1,2,2,3,3,4] 17 | 3:木三局:[4,1,2,5,2,3,6,3,4,7,4,5,8,5,6,9,6,7,10,7,8,11,8,9,0,9,10,1,10,11] 18 | 4:金四局:[11,4,1,2,0,5,2,3,1,6,3,4,2,7,4,5,3,8,5,6,4,9,6,7,5,10,7,8,6,11] 19 | 2:土五局:[6,11,4,1,2,7,0,5,2,3,8,1,6,3,4,9,2,7,4,5,10,3,8,5,6,11,4,9,6,7] 20 | 1:火六局:[9,6,11,4,1,2,10,7,0,5,2,3,11,8,1,6,3,4,0,9,2,7,4,5,1,10,3,8,5,6]*/ 21 | var FiveEleTable=[ 22 | [1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,0,0,1,1,2,2,3,3,4], 23 | [9,6,11,4,1,2,10,7,0,5,2,3,11,8,1,6,3,4,0,9,2,7,4,5,1,10,3,8,5,6], 24 | [6,11,4,1,2,7,0,5,2,3,8,1,6,3,4,9,2,7,4,5,10,3,8,5,6,11,4,9,6,7], 25 | [4,1,2,5,2,3,6,3,4,7,4,5,8,5,6,9,6,7,10,7,8,11,8,9,0,9,10,1,10,11], 26 | [11,4,1,2,0,5,2,3,1,6,3,4,2,7,4,5,3,8,5,6,4,9,6,7,5,10,7,8,6,11]]; 27 | var FiveEleArr=[[0,1,3,2,4,1],[1,2,4,3,0,2],[2,3,0,4,1,3],[3,4,1,0,2,4],[4,0,2,1,3,0]]; 28 | 29 | //StarM_A14 => 0:紫微,1:天機,2:太陽,3:武曲,4:天同,5:廉貞,6:天府,7:太陰,8:貪狼,9:巨門,10:天相,11:天梁,12:七殺,13:破軍 30 | var Star_A14=[ 31 | [[0],[],[13],[],[5,6],[7],[8],[4,9],[3,10],[2,11],[12],[1]], 32 | [[1],[0,13],[],[6],[7],[5,8],[9],[10],[4,11],[3,12],[2],[]], 33 | [[13],[1],[0,6],[7],[8],[9],[5,10],[11],[12],[10],[3],[2]], 34 | [[2],[6],[1,7],[0,8],[9],[10],[11],[5,12],[],[],[4],[3,13]], 35 | [[3,6],[2,7],[8],[1,9],[0,10],[11],[12],[],[5],[],[13],[4]], 36 | [[4,7],[3,8],[2,9],[10],[1,10],[0,12],[],[],[],[5,13],[],[6]], 37 | [[8],[4,9],[3,10],[2,11],[12],[1],[0],[],[13],[],[5,6],[7]], 38 | [[9],[10],[4,11],[3,12],[2],[],[1],[0,13],[],[6],[7],[5,8]], 39 | [[5,10],[11],[12],[10],[3],[2],[13],[1],[0,6],[7],[8],[9]], 40 | [[11],[5,12],[],[],[4],[3,13],[2],[6],[1,7],[0,8],[9],[10]], 41 | [[12],[],[5],[],[13],[4],[3,6],[2,7],[8],[1,9],[0,10],[11]], 42 | [[],[],[],[5,13],[],[6],[4,7],[3,8],[2,9],[10],[1,10],[0,12]] 43 | ]; 44 | //安紫微諸星表,第7列,[6]為天府 45 | var Star_Z06=[ 46 | [0,1,2,3,4,5,6,7,8,9,10,11], 47 | [11,0,1,2,3,4,5,6,7,8,9,10], 48 | [9,10,11,0,1,2,3,4,5,6,7,8], 49 | [8,9,10,11,0,1,2,3,4,5,6,7], 50 | [7,8,9,10,11,0,1,2,3,4,5,6], 51 | [4,5,6,7,8,9,10,11,0,1,2,3], 52 | [4,3,2,1,0,11,10,9,8,7,6,5] 53 | ]; 54 | var Star_T08=[ 55 | [0,1,2,3,4,5,6,7,8,9,10,11], 56 | [1,2,3,4,5,6,7,8,9,10,11,0], 57 | [2,3,4,5,6,7,8,9,10,11,0,1], 58 | [3,4,5,6,7,8,9,10,11,0,1,2], 59 | [4,5,6,7,8,9,10,11,0,1,2,3], 60 | [5,6,7,8,9,10,11,0,1,2,3,4], 61 | [6,7,8,9,10,11,0,1,2,3,4,5], 62 | [10,11,0,1,2,3,4,5,6,7,8,9] 63 | ]; 64 | //StarM_A07=new Array("文昌","文曲","左輔","右弼","天魁","天鉞","祿存"); 65 | //0:文昌 1:文曲 (時) 2:左輔 3:右弼 (月) 4:天魁 5:天鉞 6:祿存(年干) 66 | var Star_G07=[ 67 | [10,9,8,7,6,5,4,3,2,1,0,11], 68 | [4,5,6,7,8,9,10,11,0,1,2,3], 69 | [4,5,6,7,8,9,10,11,0,1,2,3], 70 | [10,9,8,7,6,5,4,3,2,1,0,11], 71 | [1,0,11,11,1,0,1,6,3,3], 72 | [7,8,9,9,7,8,7,2,5,5], 73 | [2,3,5,6,5,6,8,9,11,0] 74 | ]; 75 | //StarM_S04=new Array("化祿","化權","化科","化忌"); 76 | //0:紫微,1:天機,2:太陽,3:武曲,4:天同,5:廉貞,6:天府,7:太陰,8:貪狼,9:巨門,10:天相,11:天梁,12:七殺,13:破軍 77 | //0:文昌 1:文曲 (時) 2:左輔 3:右弼 (月) 4:天魁 5:天鉞 6:祿存(年干) 78 | //0:化祿 1:化權 2:化科 3:化忌 79 | var Star_S04=[ 80 | [StarM_A14[5],StarM_A14[1],StarM_A14[4],StarM_A14[7],StarM_A14[8],StarM_A14[3],StarM_A14[2],StarM_A14[9],StarM_A14[11],StarM_A14[13]], 81 | [StarM_A14[13],StarM_A14[11],StarM_A14[1],StarM_A14[4],StarM_A14[7],StarM_A14[8],StarM_A14[3],StarM_A14[2],StarM_A14[0],StarM_A14[9]], 82 | [StarM_A14[3],StarM_A14[0],StarM_A07[0],StarM_A14[1],StarM_A07[3],StarM_A14[11],StarM_A14[7],StarM_A07[1],StarM_A07[2],StarM_A14[7]], 83 | [StarM_A14[2],StarM_A14[7],StarM_A14[5],StarM_A14[9],StarM_A14[1],StarM_A07[1],StarM_A14[4],StarM_A07[0],StarM_A14[3],StarM_A14[8]] 84 | ]; 85 | //StarM_B06=new Array("擎羊","陀羅","火星","鈴星","天空","地劫"); 86 | //0:擎羊 1:陀羅 2:火星 3:鈴星 4:天空 5:地劫 87 | //0:2,6,10 1:8,0,4 2:5,9,1 3:11,3,7 %4=> 2,0,1,3 88 | //改成0:8,0,4 1:5,9,1 2:2,6,10 3:11,3,7 89 | var Star_B06=[ 90 | [3,4,6,7,6,7,9,10,0,1], 91 | [1,2,4,5,4,5,7,8,10,11], 92 | [[2,3,4,5,6,7,8,9,10,11,0,1],[3,4,5,6,7,8,9,10,11,0,1,2],[1,2,3,4,5,6,7,8,9,10,11,0],[9,10,11,0,1,2,3,4,5,6,7,8]], 93 | [[10,11,0,1,2,3,4,5,6,7,8,9],[10,11,0,1,2,3,4,5,6,7,8,9],[3,4,5,6,7,8,9,10,11,0,1,2],[10,11,0,1,2,3,4,5,6,7,8,9]], 94 | [11,10,9,8,7,6,5,4,3,2,1,0], 95 | [11,0,1,2,3,4,5,6,7,8,9,10] 96 | ]; 97 | //StarO_S05=new Array("天馬","龍池","鳳閣","紅鸞","天喜""); 98 | var Star_OS5=[ 99 | [2,11,8,5,2,11,8,5,2,11,8,5], 100 | [4,5,6,7,8,9,10,11,0,1,2,3], 101 | [10,9,8,7,6,5,4,3,2,1,0,11], 102 | [3,2,1,0,11,10,9,8,7,6,5,4], 103 | [9,8,7,6,5,4,3,2,1,0,11,10] 104 | ]; 105 | -------------------------------------------------------------------------------- /js/ziweicore.js: -------------------------------------------------------------------------------- 1 | /*紫微斗數 Chinese Astrology Zi Wei Dou Shu*/ 2 | var ziwei = { 3 | y:null, m:null, d:null, h:null, g:null, l:null, b:null, f:null, s4:null, z:null, 4 | yS:null, mS:null, dS:null, LunarDay:null, ShengXiao:null, 5 | y1Pos:null, y2Pos:null, hPos:null, lPos:null, bPos:null, zPos:null, Palce:null, 6 | Place12:null, 7 | //排紫微命盤 8 | computeZiWei:function (y_Solar,m_Solar,d_Solar,h_Solar,g_Solar){ 9 | //y:年,m:月,d:日,h:時,g:性別,l:命宮,b:身宮,f:五行局,s:起紫微表,s4:四化星; 10 | yS=y_Solar; 11 | mS=m_Solar; 12 | dS=d_Solar; 13 | //取得農曆時辰,排紫微命盤 14 | Lunar(0,yS,mS,dS); 15 | y=HeavenlyStems[(yS - 4) % 10]+EarthlyBranches[(yS - 4) % 12]; 16 | m=lunar.m; 17 | d=lunar.d; 18 | h=h_Solar; 19 | g=g_Solar; 20 | //年:天干地支 21 | y1Pos=HeavenlyStems.indexOf(y.substring(0,1)); 22 | y2Pos=EarthlyBranches.indexOf(y.substring(1,2)); 23 | //時:地支 24 | hPos=EarthlyBranches.indexOf(h); 25 | //設定紫微斗數 26 | this.setZiwei(d); 27 | //stepSetStar 28 | this.stepSetStar(y,m,d,h); 29 | return Place12; 30 | }, 31 | //CenterPart↑↓ 32 | getLunarDay:function(){return GanGB[gan.y]+ZhiGB[zhi.y]+" 年 "+(lunar.l?"閏":"")+lunar.m+" 月 "+lunar.d+" 日 " + h + " 時";}, 33 | getSolarDay:function(){return yS+ " 年 " + mS + " 月 " + dS + " 日" + h + " 時";}, 34 | getShengXiao:function(){return ShengXiaoGB[(yS - 4) % 12];}, 35 | getFiveElement:function(){return f;}, 36 | getYinYangGender:function(){return YinYang[y1Pos%2]+(g=="M"?"男":"女");}, 37 | //CenterPart↑ 38 | getStarArr:function (STAR,size,pos){ 39 | var starArray = new Array(); 40 | for (i=0;i=0)?""+StarM_S04[STAR.indexOf(starName)].substring(1,2)+"":" "; 48 | }, 49 | getS04Str:function (starName,STAR){ 50 | return (STAR.indexOf(starName)>=0)?StarM_S04[STAR.indexOf(starName)]:""; 51 | }, 52 | setZiwei:function (d){ 53 | //重排宮位 54 | ziweiUI.clearPalce(); 55 | //安十二宮,安命宮、身宮 56 | l=EarthlyBranches[((12-hPos)+1+m*1.0)%12]; 57 | b=EarthlyBranches[(12-((22-hPos)+1-m*1.0)%12)%12]; 58 | lPos=EarthlyBranches.indexOf(l); 59 | bPos=EarthlyBranches.indexOf(b); 60 | //安五行局 61 | f=FiveElements[FiveEleArr[y1Pos%5][((lPos-(lPos%2==0?0:1))/2)%6]]; 62 | //起紫微表 63 | z=EarthlyBranches[FiveEleTable[FiveElements.indexOf(f)][d-1]]; 64 | zPos=EarthlyBranches.indexOf(z); 65 | }, 66 | stepSetStar:function (y,m,d,h){ 67 | //準備星星 68 | //0:紫微,1:天機,2:太陽,3:武曲,4:天同,5:廉貞,6:天府,7:太陰,8:貪狼,9:巨門,10:天相,11:天梁,12:七殺,13:破軍 69 | var s14=Star_A14[zPos]; 70 | var sZ06=this.getStarArr(Star_Z06,7,zPos); 71 | var sT08=this.getStarArr(Star_T08,8,sZ06[6]); 72 | //0:文昌 1:文曲 (時) 2:左輔 3:右弼 (月) 4:天魁 5:天鉞 6:祿存(年干) 73 | var sG07=this.getStarArrByPosArr(Star_G07,7,[hPos,hPos,m-1,m-1,y1Pos,y1Pos,y1Pos]); 74 | //四化星 75 | var sS04=this.getStarArr(Star_S04,4,y1Pos); 76 | //六凶星 77 | var sB06=[Star_B06[0][y1Pos],Star_B06[1][y1Pos],Star_B06[2][y2Pos%4][hPos],Star_B06[3][y2Pos%4][hPos],Star_B06[4][hPos],Star_B06[5][hPos] ]; 78 | //其他 79 | var OS05=this.getStarArr(Star_OS5,5,y2Pos); 80 | Place12=new Array(12); 81 | //準備開始組星星 82 | for (i=0;i<12;i++){ 83 | var StarA,StarB,StarC,Star6,lenStar=[0,0,0,0]; 84 | StarA=[];StarB=[];StarC=[];Star6=[]; 85 | //紫微星系 & 六凶星 86 | for (k=0;k<6;k++){ 87 | if (sZ06[k]==i){ StarA[lenStar[0]]=StarM_A14[k]+this.getS04Str(StarM_A14[k],sS04); lenStar[0]+=1; } 88 | if (sB06[k]==i){ StarB[lenStar[1]]=StarM_B06[k]; lenStar[1]+=1;} 89 | } 90 | //天府星系 91 | for (k=0;k<8;k++){ 92 | if (sT08[k]==i){ StarA[lenStar[0]]=StarM_A14[k+6]+this.getS04Str(StarM_A14[k+6],sS04); lenStar[0]+=1; } 93 | } 94 | //六吉星 95 | for (k=0;k<7;k++){ 96 | if (sG07[k]==i){ Star6[lenStar[3]]=StarM_A07[k]+this.getS04Str(StarM_A07[k],sS04); lenStar[3]+=1; } 97 | } 98 | //其他星矅StarO_S0.length 99 | for (k=0;k<5;k++){ 100 | if (OS05[k]==i){ StarC[lenStar[2]]=StarO_S05[k]; lenStar[2]+=1;} 101 | } 102 | //塞入位置 103 | Place12[i]={ 104 | "MangA": HeavenlyStems[((y1Pos%5)*2+(i<2?i+2:i)%10)%10]+"
"+EarthlyBranches[i], 105 | "MangB": Palace[(12-lPos+i)%12], 106 | "MangC": (bPos==i?Palace[12]:""), 107 | "StarA": StarA,"StarB": StarB,"StarC": StarC,"Star6": Star6 108 | }; 109 | } 110 | }, 111 | getDaShian:function (){ 112 | //大限資料準備 113 | var DaShianYear=DaShian[FiveElements.indexOf(f)]; 114 | var MangDirection=y1Pos%2; MangDirection+=(g=="M")?1:0; 115 | //小限資料準備 116 | var SiaoShianYear,SiaoShianStr,SiaoPlace; 117 | switch(y2Pos){ 118 | case 2: case 6: case 10: SiaoShianYear=4; break; 119 | case 8: case 0: case 4: SiaoShianYear=11; break; 120 | case 5: case 9: case 1: SiaoShianYear=7; break; 121 | case 11: case 3: case 7: SiaoShianYear=1; break; 122 | } 123 | var DShian=new Array(10); 124 | var SShian=new Array(10); 125 | for (i=0;i<12;i++){ 126 | //起大限表 127 | var DSY=MangDirection==1?DaShianYear+i*10:(DaShianYear-i*10+120)%120; 128 | //起小限表 129 | SiaoShianStr=""; 130 | for (j=0;j<6;j++){ 131 | SiaoShianStr+=","+(j*12+i+1); 132 | } 133 | SiaoShianStr=SiaoShianStr.substring(1) + " ... "; 134 | SiaoPlace=(g=="M")?((SiaoShianYear+i)%12+1).toString():((SiaoShianYear-i+12)%12+1).toString(); 135 | DShian[(i+lPos)%12+1]=DSY.toString()+" - "+(DSY+9).toString(); 136 | SShian[SiaoPlace]=SiaoShianStr; 137 | } 138 | return {"DShian":DShian,"SShian":SShian}; 139 | } 140 | }; 141 | -------------------------------------------------------------------------------- /js/ziweiui.js: -------------------------------------------------------------------------------- 1 | /*紫微斗數 Chinese Astrology Zi Wei Dou Shu*/ 2 | var ziweiUI = { 3 | //主星列印方向 true:由右向左,false:由左向右 4 | right2left:false, 5 | //UI resize 6 | resize:function (){ 7 | //var wdth=$(window).width(); 8 | //$("#divZiWei").css("left",wdth>640?(wdth-640)/2:0); 9 | }, 10 | genNowDateZiwei:function (){ 11 | this.getNowDate(); 12 | this.genZiwei(); 13 | }, 14 | getNowDate:function(){ 15 | var Today=new Date(); 16 | var h=Today.getHours(); 17 | Today.setDate(Today.getDate()+(h>=23?1:0)); 18 | document.getElementById("sel_Year").value=Today.getFullYear(); 19 | document.getElementById("sel_Month").value=Today.getMonth()+1; 20 | document.getElementById("sel_Day").value=Today.getDate(); 21 | document.getElementById("sel_Hour").value=EarthlyBranches[(h+(h%2?1:0))%24/2]; 22 | }, 23 | //initial 24 | initial:function (){ 25 | //畫紫微斗數空表格 26 | //document.getElementById("container").innerHTML="

紫微斗數命盤

西元
"; 27 | document.getElementById("container").innerHTML="

紫微斗數命盤

西元
"; 28 | function addOption(id,a,b){ 29 | for (i=a;i<=b;i++){ 30 | let op = document.createElement('option'); 31 | op.value = i; 32 | op.innerHTML = i; 33 | document.getElementById(id).appendChild(op); 34 | } 35 | } 36 | addOption("sel_Year",1900,2049); 37 | addOption("sel_Month",1,12); 38 | addOption("sel_Day",1,31); 39 | for (i=0;i"; 52 | } 53 | }, 54 | cleanZiwei:function (){ 55 | //$("#zwHome").html(""); 56 | document.getElementById("zwHome").innerHTML = ""; 57 | this.clearPalce(); 58 | }, 59 | genZiwei:function(){ 60 | 61 | let gender= document.querySelectorAll("input[type=radio]"); 62 | let genderValue="M"; 63 | for (i=0;i" 71 | + "農曆:" + ziwei.getLunarDay()+ "
" 72 | + "生肖:【" + ziwei.getShengXiao() + "】"+"
" 73 | + "
"+ ziwei.getFiveElement() +"
" 74 | + "
"+ ziwei.getYinYangGender()+"
" 75 | + "
by cubshuang
"; 76 | //render Direction 77 | var styleLR=[" zwStarLeft"," zwStarRight"]; 78 | if(this.right2left){ styleLR.reverse(); } 79 | //render Star 80 | for (i=0;i<12;i++){ 81 | document.getElementById("zw"+(i+1).toString()).innerHTML+= 82 | "
" + zw[i].MangA + "
" + 83 | "
" + zw[i].MangB + "
" + 84 | "
" + zw[i].MangC + "
" + 85 | "
" + zw[i].StarAll + "
" ; 86 | var StarA1,StarA2,StarA3,StarB1,StarB2,StarC1,StarC2; 87 | StarA1="";StarA2="";StarA3="";StarB1="";StarB2="";StarC1="";StarC2=""; 88 | var tmpSatrA=[[],[],[]]; 89 | var k=0; 90 | for (j=0;j2)?""+zw[i].StarA[j].substring(3,4)+"":" "; 94 | k+=1; 95 | } 96 | for (j=0;j" 98 | tmpSatrA[1][k]=""+zw[i].Star6[j].substring(1,2)+"" 99 | tmpSatrA[2][k]=(zw[i].Star6[j].length>2)?""+zw[i].Star6[j].substring(3,4)+"":" "; 100 | k+=1; 101 | } 102 | //style Left or Right 103 | if(this.right2left){ 104 | for(j=0;j<3;j++){ tmpSatrA[j].reverse(); } 105 | } 106 | //render StarA & B & C 107 | for (j=0;j" + StarA1+ "
"+StarA2 + "
"+StarA3 + "
" + 122 | "
" + StarB1+ "
"+StarB2 + "
" + 123 | "
" + StarC1+ "
"+StarC2 + "
"; 124 | } 125 | //大小限表 126 | var DS_Shian=ziwei.getDaShian(); 127 | for (i=0;i<12;i++){ 128 | document.getElementById("zw"+(i+1).toString()).innerHTML+= 129 | "
"+ DS_Shian.DShian[i+1] + "
" + 130 | "
" + DS_Shian.SShian[i+1] + "
" ; 131 | } 132 | } 133 | }; 134 | window.addEventListener('load' ,function(){ 135 | //開始使用 136 | ziweiUI.initial(); 137 | document.getElementById("btnNowDate").addEventListener('click',function () {ziweiUI.genNowDateZiwei();}); 138 | let s = document.querySelectorAll("select"); 139 | for (i=0;i陰 272 | answer="陰曆"+lunar.y+"("+GanGB[gan.y]+ZhiGB[zhi.y]+")年"+(lunar.l?"閏":"")+lunar.m+"月"+lunar.d+"日,生肖屬"+ShengXiaoGB[zhi.y]; 273 | // answer2="干支:"+GanGB[gan.y]+ZhiGB[zhi.y]+"年"+ 274 | // GanGB[gan.m]+ZhiGB[zhi.m]+"月"+GanGB[gan.d]+ZhiGB[zhi.d]+"日"; 275 | } 276 | else { 277 | answer="陽曆"+solar.y+"年"+solar.m+"月"+solar.d+"日,星期"+weekdayGB[solar.w]; 278 | // answer2="干支:"+GanGB[gan.y]+ZhiGB[zhi.y]+"年"+ 279 | // GanGB[gan.m]+ZhiGB[zhi.m]+"月"+GanGB[gan.d]+ZhiGB[zhi.d]+"日"; 280 | } 281 | // answer2=title+lunar.y+"年"+lunar.m+"月"+lunar.d+"日,星期"+lunar.w; 282 | // alert(answer); 283 | ans.value=answer; 284 | // ans2.value=answer2; 285 | } 286 | ----------------------------------------------------------*/ 287 | function LeapYear(y) { 288 | return ( ((y%4)==0) && ((y%100)!=0) || ((y%400)==0) ); 289 | } 290 | function solar2Day1(d) {//d:MyDate type 291 | var offset,delta; 292 | var i; 293 | delta=d.y-BYEAR;//BYEAR 294 | if(delta<0) { 295 | alert("internal error:pick a larger constant for BYEAR"); 296 | return -1; 297 | } 298 | offset=Math.floor(delta*365)+Math.floor(delta/4)-Math.floor(delta/100)+ 299 | Math.floor(delta/400); 300 | for(i=1;i2) && (LeapYear(d.y))) offset++; 304 | offset+=d.d-1; 305 | if((d.m==2) && LeapYear(d.y)) { 306 | if(d.d>29) { 307 | alert("day1 out of range"); 308 | return -1; 309 | } 310 | } 311 | else if(d.d>daysInSolarMonth[d.m]) { 312 | alert("day2 out of range"); 313 | return -1; 314 | } 315 | return offset; 316 | } 317 | /*----------------------------------------------------------*/ 318 | function solar2Day(d) {//MyDate type 319 | // alert("d="+d); 320 | // alert("1:"+solar2Day1(d)+"\n2:"+solar2Day1(solarFirstDate)); 321 | return (solar2Day1(d)-solar2Day1(solarFirstDate)); 322 | } 323 | /*----------------------------------------------------------*/ 324 | /* Compute the number of days in each lunar year in the table */ 325 | function make_yday() 326 | { 327 | var year, i, leap; 328 | var code; 329 | 330 | for (year = 0; year < Nyear; year++) { 331 | code = yearInfo[year]; 332 | leap = code & 0xf; 333 | yday[year] = 0; 334 | if (leap != 0) { 335 | i = (code >> 16) & 0x1; 336 | yday[year] += moon[i]; 337 | } 338 | code >>= 4; 339 | for (i = 0; i < Nmonth-1; i++) { 340 | yday[year] += moon[code&0x1]; 341 | code >>= 1; 342 | } 343 | ymonth[year] = 12; 344 | if (leap != 0) ymonth[year]++; 345 | } 346 | return Nyear; 347 | } 348 | /*----------------------------------------------------------*/ 349 | /* Compute the days of each month in the given lunar year */ 350 | function make_mday(year) 351 | { 352 | var i, leapMonth,code; 353 | 354 | code = yearInfo[year]; 355 | leapMonth = code & 0xf; 356 | /* leapMonth == 0 means no leap month */ 357 | code >>= 4; 358 | if (leapMonth == 0) 359 | { 360 | mday[Nmonth] = 0; 361 | for (i = Nmonth-1; i >= 1; i--) 362 | { 363 | mday[i] = moon[code&0x1]; 364 | code >>= 1; 365 | } 366 | } 367 | else 368 | { 369 | /* 370 | There is a leap month (run4 yue4) L in this year. 371 | mday[1] contains the number of days in the 1-st month; 372 | mday[L] contains the number of days in the L-th month; 373 | mday[L+1] contains the number of days in the L-th leap month; 374 | mday[L+2] contains the number of days in the L+1 month, etc. 375 | 376 | cf. yearInfo[]: info about the leap month is encoded differently. 377 | */ 378 | i = (yearInfo[year] >> 16) & 0x1; 379 | mday[leapMonth+1] = moon[i]; 380 | for (i = Nmonth; i >= 1; i--) 381 | { 382 | if (i == leapMonth+1) i--; 383 | mday[i] = moon[code&0x1]; 384 | code >>= 1; 385 | } 386 | } 387 | return leapMonth; 388 | } 389 | /*----------------------------------------------------------*/ 390 | function day2Lunar(offset,d) { 391 | var i,m,nYear,leapMonth; 392 | 393 | nYear = make_yday(); 394 | for (i=0; i 0; i++) offset -= yday[i]; 395 | 396 | if (offset<0) offset += yday[--i]; 397 | 398 | if (i==Nyear) { 399 | alert("Year out of range."); 400 | return ; 401 | } 402 | 403 | d.y = i + LunarFirstDate.y; 404 | 405 | leapMonth = make_mday(i); 406 | for (m=1; m<=Nmonth && offset>0; m++) offset -= mday[m]; 407 | 408 | if (offset<0) offset += mday[--m]; 409 | 410 | d.l= 0; /* don't know leap or not yet */ 411 | 412 | if (leapMonth>0) { /* has leap month */ 413 | // if preceeding month number is the leap month, this month is the actual extra leap month 414 | d.l = (leapMonth == (m - 1)); 415 | /* month > leapMonth is off by 1, so adjust it */ 416 | if (m > leapMonth) --m; 417 | } 418 | 419 | d.m = m; 420 | d.d = offset + 1; 421 | } 422 | /*----------------------------------------------------------*/ 423 | function CalGZ(offset, d, g, z) 424 | { 425 | var year, month; 426 | 427 | year = d.y - LunarFirstDate.y; 428 | month = year * 12 + d.m - 1; /* leap months do not count */ 429 | 430 | // alert("year="+year+"month="+month); 431 | g.y = (GanFirstDate.y + year) % 10; 432 | z.y = (ZhiFirstDate.y + year) % 12; 433 | g.m = (GanFirstDate.m + month) % 10; 434 | z.m = (ZhiFirstDate.m + month) % 12; 435 | g.d = (GanFirstDate.d + offset) % 10; 436 | z.d = (ZhiFirstDate.d + offset) % 12; 437 | z.h = Math.floor((d.h + 1) / 2) % 12; 438 | g.h = (g.d * 12 + z.h) % 10; 439 | } 440 | /*----------------------------------------------------------*/ 441 | /* Compare two dates and return <,=,> 0 if the 1st is <,=,> the 2nd */ 442 | function CmpDate(month1, day1, month2, day2) 443 | { 444 | if (month1!=month2) return(month1-month2); 445 | if (day1!=day2) return(day1-day2); 446 | return(0); 447 | } 448 | /*----------------------------------------------------------*/ 449 | /* Given a solar date, find the "lunar" date for the purpose of 450 | calculating the "4-columns" by taking jie into consideration. 451 | */ 452 | function JieDate(ds, dl)//MyDate type 453 | { 454 | var m, flag; 455 | 456 | if (ds.m==1) { 457 | flag = CmpDate(ds.m, ds.d, 458 | 1, fest[(ds.y - solarFirstDate.y - 1)*12+11]); 459 | if (flag<0) dl.m = 11; 460 | else if (flag>0) dl.m = 12; 461 | dl.y = ds.y - 1; 462 | return(flag==0); 463 | } 464 | for (m=2; m<=12; m++) { 465 | flag = CmpDate(ds.m, ds.d, 466 | m, fest[(ds.y - solarFirstDate.y)*12+m-2]); 467 | if (flag==0) m++; 468 | if (flag<=0) break; 469 | } 470 | dl.m = (m-2) % 12; 471 | dl.y = ds.y; 472 | if ((dl.m)==0){ 473 | dl.y = ds.y - 1; 474 | dl.m = 12; 475 | } 476 | return(flag==0); 477 | } 478 | /*----------------------------------------------------------*/ 479 | function solar2Lunar() { 480 | var offset; 481 | offset=solar2Day(solar); 482 | 483 | solar.w=(offset+solarFirstDate.w)%7; 484 | //alert("offset="+offset+",w="+solar.w); 485 | //a lunar day begins at 11pm 486 | if(solar.h==23) offset++; 487 | 488 | day2Lunar(offset,lunar); 489 | lunar.h=solar.h; 490 | CalGZ(offset,lunar,gan,zhi); 491 | 492 | jieAlert = JieDate(solar, lunar2); 493 | lunar2.d = lunar.d; 494 | lunar2.hour = lunar.hour; 495 | CalGZ(offset, lunar2, gan2, zhi2); 496 | } 497 | /*----------------------------------------------------------*/ 498 | /* Compute offset days of a lunar date from the beginning of the table */ 499 | function Lunar2Day(d) 500 | { 501 | var offset = 0; 502 | var year, i, m, nYear, leapMonth; 503 | 504 | nYear = make_yday(); 505 | year = d.y - LunarFirstDate.y; 506 | for (i=0; imonth, d->year); 513 | // exit(1); 514 | } 515 | for (m=1; mleapMonth) || (d.l && (d.m==leapMonth)))) 517 | offset += mday[m++]; 518 | offset += d.d- 1; 519 | 520 | if (d.d > mday[m]) Error("Day out of range."); 521 | 522 | return offset; 523 | } 524 | /*----------------------------------------------------------*/ 525 | function Solar2Day(d) 526 | { 527 | return (Solar2Day1(d) - Solar2Day1(solarFirstDate)); 528 | } 529 | /*----------------------------------------------------------*/ 530 | /* Compute the number of days from the Solar date BYEAR.1.1 */ 531 | function Solar2Day1(d) 532 | { 533 | var offset, delta; 534 | var i; 535 | 536 | delta = d.y - BYEAR; 537 | if (delta<0) { 538 | //Error("Internal error: pick a larger constant for BYEAR."); 539 | alert("Internal error: pick a larger constant for BYEAR."); 540 | return -1; 541 | } 542 | offset = delta * 365 + Math.floor(delta / 4) - Math.floor(delta / 100) + 543 | Math.floor(delta / 400); 544 | for (i=1; i< d.m; i++) 545 | offset += daysInSolarMonth[i]; 546 | if ((d.m > 2) && LeapYear(d.y)) offset++; 547 | offset += d.d - 1; 548 | 549 | if ((d.m == 2) && LeapYear(d.y)) { 550 | if (d.d > 29){ 551 | // Error("Day out of range."); 552 | alert("Day out of range."); 553 | return -1; 554 | } 555 | } 556 | else if (d.d > daysInSolarMonth[d.m]) { 557 | // Error("Day out of range."); 558 | alert("Day out of range."); 559 | return -1; 560 | } 561 | return offset; 562 | } 563 | /*----------------------------------------------------------*/ 564 | function Day2Solar(offset, d) 565 | { 566 | var i, m, days; 567 | 568 | /* offset is the number of days from SolarFirstDate */ 569 | offset -= Solar2Day(LunarFirstDate); /* the argument is negative */ 570 | /* offset is now the number of days from SolarFirstDate.year.1.1 */ 571 | 572 | for (i=solarFirstDate.y;(i 0); i++) 573 | offset -= 365 + LeapYear(i); 574 | if (offset<0) { 575 | --i; /* LeapYear is a macro */ 576 | offset += 365 + LeapYear(i); 577 | } 578 | if (i==(solarFirstDate.y + Nyear)) { 579 | //Error("Year out of range."); 580 | alert("Year out of range."); 581 | return ; 582 | } 583 | d.y = i; 584 | 585 | /* assert(offset<(365+LeapYear(i))); */ 586 | for (m=1; m<=12; m++) { 587 | days = daysInSolarMonth[m]; 588 | if ((m==2) && LeapYear(i)) /* leap February */ 589 | days++; 590 | if (offset陰 621 | //type=1,陰->陽 622 | function Lunar(type,my_y,my_m,my_d) 623 | { 624 | if(type==0) {//陽->陰 625 | solar.y=my_y; 626 | solar.m=my_m; 627 | solar.d=my_d; 628 | title="陰曆:"; 629 | solar2Lunar(); 630 | } 631 | else {//陰->陽 632 | lunar.y=my_y; 633 | lunar.m=my_m; 634 | lunar.d=my_d; 635 | title="陽曆:"; 636 | Lunar2Solar(); 637 | } 638 | } 639 | --------------------------------------------------------------------------------