天干地支八字排盘JS版源码

在上一篇日记中,波波从网络上搜集整理了一些天干地支的计算规则,在本篇笔记中波波将通过JS代码片段来分享天干地支八字排盘规则在算命程序中的应用。

天干地支八字排盘JS源码:

  1. function Bazi(y,m,d,h,sex){
  2.     var y=Number(y);
  3.     var m=m-1;
  4.     var d=Number(d);
  5.     var h=Number(h);
  6.     var sex=Number(sex);
  7.     var y_d=new Date();
  8.         y_d.setFullYear(y);
  9.         y_d.setMonth(m);
  10.         y_d.setDate(d);
  11.         y_d.setHours(h);
  12.         y_d.setMinutes(0);
  13.         y_d.setSeconds(0);
  14.     var y_t=y_d.getTime();
  15.     var tg=["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"];
  16.     var dz=["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"];
  17.     var dz0=["丑","寅","卯","辰","巳","午","未","申","酉","戌","亥","子"];
  18.     var jq=["小寒","立春","惊蛰","清明","立夏","芒种","小暑","立秋","白露","寒露","立冬","大雪"];
  19.     var jq84=[442208451146,444755924716,447326679845,449936540593,452591457618,455285317308,458000946032,460714673166,463403390187,466051355952,468654332864,471220083199];
  20.     var y_d84=441734400726;
  21.     var zsss={
  22.         甲:["沐浴","冠带","临官","帝旺","衰","病","死","墓","绝","胎","养","长生"],
  23.         乙:["病","衰","帝旺","临官","冠带","沐浴","长生","养","胎","绝","墓","死"],
  24.         丙:["胎","养","长生","沐浴","冠带","临官","帝旺","衰","病","死","墓","绝"],
  25.         丁:["绝","墓","死","病","衰","帝旺","临官","冠带","沐浴","长生","养","胎"],
  26.         戊:["胎","养","长生","沐浴","冠带","临官","帝旺","衰","病","死","墓","绝"],
  27.         己:["绝","墓","死","病","衰","帝旺","临官","冠带","沐浴","长生","养","胎"],
  28.         庚:["死","墓","绝","胎","养","长生","沐浴","冠带","临官","帝旺","衰","病"],
  29.         辛:["长生","养","胎","绝","墓","死","病","衰","帝旺","临官","冠带","沐浴"],
  30.         壬:["帝旺","衰","病","死","墓","绝","胎","养","长生","沐浴","冠带","临官"],
  31.         癸:["临官","冠带","沐浴","长生","养","胎","绝","墓","死","病","衰","帝旺"]
  32.     }
  33.     var nayi={
  34.         甲子:["海中金"],乙丑:["海中金"],丙寅:["炉中火"],丁卯:["炉中火"],戊辰:["大林木"],己巳:["大林木"],庚午:["路旁土"],辛未:["路旁土"],壬申:["剑锋金"],癸酉:["剑锋金"],
  35.         甲戌:["山头火"],乙亥:["山头火"],丙子:["涧下水"],丁丑:["涧下水"],戊寅:["城墙土"],己卯:["城墙土"],庚辰:["白腊金"],辛巳:["白腊金"],壬午:["杨柳木"],癸未:["杨柳木"],
  36.         甲申:["泉中水"],乙酉:["泉中水"],丙戌:["屋上土"],丁亥:["屋上土"],戊子:["霹雳火"],己丑:["霹雳火"],庚寅:["松柏木"],辛卯:["松柏木"],壬辰:["长流水"],癸巳:["长流水"],
  37.         甲午:["沙中金"],乙未:["沙中金"],丙申:["山下火"],丁酉:["山下火"],戊戌:["平地木"],己亥:["平地木"],庚子:["壁上土"],辛丑:["壁上土"],壬寅:["金箔金"],癸卯:["金箔金"],
  38.         甲辰:["覆灯火"],乙巳:["覆灯火"],丙午:["天河水"],丁未:["天河水"],戊申:["大驿土"],己酉:["大驿土"],庚戌:["钗钏金"],辛亥:["钗钏金"],壬子:["桑柘木"],癸丑:["桑柘木"],
  39.         甲寅:["大溪水"],乙卯:["大溪水"],丙辰:["沙中土"],丁巳:["沙中土"],戊午:["天上火"],己未:["天上火"],庚申:["石榴木"],辛酉:["石榴木"],壬戌:["大海水"],癸亥:["大海水"]
  40.     }
  41.     var cangdun={
  42.         子:["癸",48],
  43.         丑:["己",16,"癸",8,"辛",4],
  44.         寅:["甲",32,"丙",16,"戊",8],
  45.         卯:["乙",48],
  46.         辰:["戊",16,"乙",8,"壬",8,],
  47.         巳:["丙",32,"庚",8,"戊",8],
  48.         午:["丁",48,"己",24],
  49.         未:["己",32,"丁",8,"乙",8],
  50.         申:["庚",32,"壬",16,"戊",8],
  51.         酉:["辛",48],
  52.         戌:["戊",32,"丁",8,"辛",8],
  53.         亥:["壬",32,"甲",16],
  54.     }
  55.     this.yGan=function(){  //年干
  56.         return tg[(y+6)%10];
  57.     }
  58.     this.yZhi=function(){  //年支
  59.         var nz;
  60.         if(y-1984<=0) nz=dz0[11+(y-1984)%12];
  61.         if(y-1984>0){
  62.           ((y-1984)%12-1)==-1?nz=dz0[11]:nz=dz0[(y-1984)%12-1];
  63.         }
  64.         return nz;
  65.     }
  66.     this.yZhu=function(){  //年柱
  67.         return this.yGan()+this.yZhi();
  68.     }
  69.     this.mGan=function(){  //月干
  70.         var ng=this.yGan();
  71.         var yg;
  72.         if(ng=="甲"||ng=="己") yg=tg[(1+m)%10];
  73.         if(ng=="乙"||ng=="庚") yg=tg[(3+m)%10];
  74.         if(ng=="丙"||ng=="辛") yg=tg[(5+m)%10];
  75.         if(ng=="丁"||ng=="壬") yg=tg[(7+m)%10];
  76.         if(ng=="戊"||ng=="癸") yg=tg[(9+m)%10];
  77.         if(y_t<((y-1984)*31556926009+jq84[m])){
  78.             (tg.indexOf(yg)-1)==-1?yg=tg[9]:yg=tg[tg.indexOf(yg)-1];
  79.         }
  80.         return yg;
  81.     }
  82.     this.mZhi=function(){  //月支
  83.         var yz=dz0[m];
  84.         if(y_t<((y-1984)*31556926009+jq84[m])){
  85.             yz=dz0[dz.indexOf(yz)-1];
  86.         }
  87.         return yz;
  88.     }
  89.     this.mZhu=function(){  //月柱
  90.         return this.mGan()+this.mZhi();
  91.     }
  92.     this.dGan=function(){  //日干
  93.         var y_r=Math.floor((y_t-y_d84)/86400000)%60;
  94.         var rg;
  95.         y_r>=0?rg=tg[y_r%10]:rg=tg[(4+(60+y_r)%10)%10];
  96.         return rg;
  97.     }
  98.     this.dZhi=function(){  //日支
  99.         var y_r=Math.floor((y_t-y_d84)/86400000)%60;
  100.         var rz;
  101.         y_r>=0?rz=dz0[(5+y_r%12)%12]:rz=dz0[(5+(60+y_r)%12)%12];
  102.         return rz;
  103.     }
  104.     this.dZhu=function(){  //日柱
  105.         return this.dGan()+this.dZhi();
  106.     }
  107.     this.hGan=function(){  //时干
  108.         var rg=this.dGan();
  109.         var sz=this.hZhi();
  110.         var sg;
  111.         if(rg=="甲"||rg=="己") sg=tg[(1+dz0.indexOf(sz))%10];
  112.         if(rg=="乙"||rg=="庚") sg=tg[(3+dz0.indexOf(sz))%10];
  113.         if(rg=="丙"||rg=="辛") sg=tg[(5+dz0.indexOf(sz))%10];
  114.         if(rg=="丁"||rg=="壬") sg=tg[(7+dz0.indexOf(sz))%10];
  115.         if(rg=="戊"||rg=="癸") sg=tg[(9+dz0.indexOf(sz))%10];
  116.         return sg;
  117.     }
  118.     this.hZhi=function(){  //时支
  119.         var sz;
  120.         if(h>=0) sz=dz0[11];
  121.         if(h>=1) sz=dz0[0];
  122.         if(h>=3) sz=dz0[1];
  123.         if(h>=5) sz=dz0[2];
  124.         if(h>=7) sz=dz0[3];
  125.         if(h>=9) sz=dz0[4];
  126.         if(h>=11) sz=dz0[5];
  127.         if(h>=13) sz=dz0[6];
  128.         if(h>=15) sz=dz0[7];
  129.         if(h>=17) sz=dz0[8];
  130.         if(h>=19) sz=dz0[9];
  131.         if(h>=21) sz=dz0[10];
  132.         if(h>=23) sz=dz0[11];
  133.         return sz;
  134.     }
  135.     this.hZhu=function(){  //时柱
  136.         return this.hGan()+this.hZhi();
  137.     }
  138.     this.xunKong=function(){  //旬空
  139.         var xtg=tg.indexOf(this.dGan());
  140.         var xdz=dz.indexOf(this.dZhi());
  141.         var xunk;
  142.         if((xtg-xdz)==0) xunk=dz[10]+dz[11];
  143.         if((xtg-xdz)==-10||(xtg-xdz)==2) xunk=dz[8]+dz[9];
  144.         if((xtg-xdz)==-8||(xtg-xdz)==4) xunk=dz[6]+dz[7];
  145.         if((xtg-xdz)==-6||(xtg-xdz)==6) xunk=dz[4]+dz[5];
  146.         if((xtg-xdz)==-4||(xtg-xdz)==8) xunk=dz[2]+dz[3];
  147.         if((xtg-xdz)==-2)   xunk=dz[0]+dz[1];
  148.         return xunk;
  149.     }
  150.     this.daYun=function(){  //大运
  151.         //  大运
  152.         var dyg=[];
  153.         var tg1=tg.concat(tg);
  154.         var dyz=[];
  155.         var dz1=dz.concat(dz);
  156.         var ng=this.yGan();
  157.         var yg=this.mGan();
  158.         var yz=this.mZhi();
  159.         var dyun=[];
  160.         //大运天干
  161.         if(((tg.indexOf(ng))%2==0 && sex==1)||((tg.indexOf(ng))%2==1 && sex==0)){
  162.             for(var n=(tg1.indexOf(yg))+1; n<20; n++){
  163.                 dyg.push(tg1[n]);
  164.                 dyg.splice(8,22);
  165.             }
  166.         }
  167.         if(((tg.indexOf(ng))%2==1 && sex==1)||((tg.indexOf(ng))%2==0 && sex==0)){
  168.             tg1.reverse();
  169.             for(var n=(tg1.indexOf(yg))+1; n<20; n++){
  170.                 dyg.push(tg1[n]);
  171.                 dyg.splice(8,22);
  172.             }
  173.         }
  174.         //大运地支
  175.         if(((tg.indexOf(ng))%2==0 && sex==1)||((tg.indexOf(ng))%2==1 && sex==0)){
  176.             for(var n=(dz1.indexOf(yz))+1; n<24; n++){
  177.                 dyz.push(dz1[n]);
  178.                 dyz.splice(8,26);
  179.             }
  180.         }
  181.         if(((tg.indexOf(ng))%2==1 && sex==1)||((tg.indexOf(ng))%2==0 && sex==0)){
  182.             dz1.reverse();
  183.             for(var n=(dz1.indexOf(yz))+1; n<24; n++){
  184.                 dyz.push(dz1[n]);
  185.                 dyz.splice(8,26);
  186.             }
  187.         }
  188.         for (i in dyg){
  189.             dyun.push(dyg[i]+dyz[i]);
  190.         }
  191.         return dyun;
  192.     }
  193.     this.qiYun=function(){  //起运
  194.         var qiy;
  195.         if(y_t<((y-1984)*31556926009+jq84[m])){
  196.           var jnianm=(((y-1984)*31556926009+jq84[m])-y_t)/259200000;
  197.           var jniann=(y_t-((y-1984)*31556926009+jq84[m-1]))/259200000;
  198.         }else{
  199.           var jnianm=(((y-1984)*31556926009+jq84[m+1])-y_t)/259200000;
  200.           var jniann=(y_t-((y-1984)*31556926009+jq84[m]))/259200000;
  201.         }
  202.         (sex==1 && y%2==0)?qiy=Math.round(jnianm):qiy=Math.round(jniann);
  203.         (sex==0 && y%2==1)?qiy=Math.round(jnianm):qiy=Math.round(jniann);
  204.         return qiy;
  205.     }
  206.     this.liuNian=function(){  //流年
  207.         var n=this.qiYun();
  208.         var l=parseInt(y)+parseInt(n);
  209.         var g,z;
  210.         var lgz=[];
  211.         for(var n=0;n<80;n++){
  212.             g=tg[(l+6)%10];
  213.             if(l-1984<=0) z=dz0[11+(l-1984)%12];
  214.             if(l-1984>0) ((l-1984)%12-1)==-1?z=dz0[11]:z=dz0[(l-1984)%12-1];
  215.             lgz.push(g+z);
  216.             l=l+1;
  217.         }
  218.         return lgz;
  219.     }
  220.     this.shiShen=function(g){  //十神
  221.         var sn;
  222.         var cn=tg.indexOf(g);
  223.         var shi=tg.indexOf(this.dGan());
  224.         if((shi%2)==0){
  225.         if((cn-shi)==-5||(cn-shi)==5) sn="财";
  226.         if((cn-shi)==-4||(cn-shi)==6) sn="杀";
  227.         if((cn-shi)==-3||(cn-shi)==7) sn="官";
  228.         if((cn-shi)==-2||(cn-shi)==8) sn="枭";
  229.         if((cn-shi)==-1||(cn-shi)==9) sn="印";
  230.         if((cn-shi)==0) sn="比";
  231.         if((cn-shi)==1||(cn-shi)==-9) sn="劫";
  232.         if((cn-shi)==2||(cn-shi)==-8) sn="食";
  233.         if((cn-shi)==3||(cn-shi)==-7) sn="伤";
  234.         if((cn-shi)==4||(cn-shi)==-6) sn="才";
  235.         }
  236.        if((shi%2)==1){
  237.         if((cn-shi)==-5||(cn-shi)==5) sn="官";
  238.         if((cn-shi)==-4||(cn-shi)==6) sn="杀";
  239.         if((cn-shi)==-3||(cn-shi)==7) sn="印";
  240.         if((cn-shi)==-2||(cn-shi)==8) sn="枭";
  241.         if((cn-shi)==-1||(cn-shi)==9) sn="劫";
  242.         if((cn-shi)==0) sn="比";
  243.         if((cn-shi)==1||(cn-shi)==-9) sn="伤";
  244.         if((cn-shi)==2||(cn-shi)==-8) sn="食";
  245.         if((cn-shi)==3||(cn-shi)==-7) sn="财";
  246.         if((cn-shi)==4||(cn-shi)==-6) sn="才";
  247.        }
  248.        return sn;
  249.     }
  250.     this.xingBie=function(){  //性别
  251.         var qk;
  252.         sex==0?qk="坤造":qk="乾造";
  253.         return qk;
  254.     }
  255.     this.naYin=function(zhu){  //纳音
  256.         var na=[];
  257.         na=na.concat(nayi[zhu]);
  258.         return na[0];
  259.     }
  260.     this.shuaiWang=function(g,z){  //旺衰
  261.         var zz=dz.indexOf(z);
  262.         var sr=zsss[g][zz];
  263.         return sr;
  264.     }
  265.     this.wuXing=function(gan){  //五行
  266.         var wux="";
  267.         if(gan=="甲"||gan=="乙") wux="木";
  268.         if(gan=="丙"||gan=="丁") wux="火";
  269.         if(gan=="戊"||gan=="己") wux="土";
  270.         if(gan=="庚"||gan=="辛") wux="金";
  271.         if(gan=="壬"||gan=="癸") wux="水";
  272.         return wux;
  273.     }
  274.     this.cangGan=function(zhi){
  275.         var z=cangdun[zhi];
  276.         console.log(z);
  277.         var zh=[];
  278.         for (var i=0;i<z.length; i++){
  279.             if(i%2==0)zh.push(z[i]);
  280.         }
  281.         return zh;
  282.     }
  283.     this.ng=this.yGan();
  284.     this.yg=this.mGan();
  285.     this.rg=this.dGan();
  286.     this.sg=this.hGan();
  287.     this.nz=this.yZhi();
  288.     this.yz=this.mZhi();
  289.     this.rz=this.dZhi();
  290.     this.sz=this.hZhi();
  291.     this.xk=this.xunKong();
  292.     this.qk=this.xingBie();
  293. }

 

你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!