我根据一篇文章,编写了一段代码用来计算年柱、月柱、日柱、时柱,但是闰月的规则我不太懂,请问有人知道怎么计算闰月吗?下面是我的代码:
//天干
var heavenlyStems = ["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"];
//地支
var earthlyBranches = ["子","丑","寅","卯","辰","巳","午","未","申","酉","戌", "亥"];
//根据公历年份计算该年的年柱
function getYearPillar(year) {
//公元3年开始为甲寅年,每隔60年重复一次
var stemIndex = (year - 3) % 10; //该年对应的天干索引
var branchIndex = (year - 3) % 12; //该年对应的地支索引
if (stemIndex < 0) stemIndex += 10; //如果索引为负数,加上10
if (branchIndex < 0) branchIndex += 12; //如果索引为负数,加上12
return heavenlyStems[stemIndex] + earthlyBranches[branchIndex]; //返回年柱
}
//根据公历月份和是否闰月计算该月的月柱
function getMonthPillar(month, leap) {
//每个月都有固定的地支,从寅开始到亥结束
var branch = earthlyBranches[(month + 1) % 12];
//根据当年的年柱来确定每个月的天干:
var stem;
switch (yearPillar[0]) { //根据年柱的第一个字符判断
case "甲":
case "己":
stem = heavenlyStems[(month +9) %10]; //从甲开始排列
break;
case "乙":
case "庚":
stem = heavenlyStems[(month +1) %10]; //从丙开始排列
break;
case "丙":
case "辛":
stem = heavenlyStems[(month +3) %10]; //从戊开始排列
break;
case "丁":
case "壬":
stem = heavenlyStems[(month +5) %10]; //从庚开始排列
break;
case "戊":
case "癸":
stem = heavenlyStems[(month +7) %10]; //从壬开始排列
break;
}
//如果是闰月,天干加一
if (leap) {
stem = heavenlyStems[(heavenlyStems.indexOf(stem) + 1) % 10];
}
return stem + branch; //返回月柱字符串
}
//根据公历日期计算该日的日柱
function getDayPillar(date) {
//公元1900年1月1日为甲戌日,每隔60天重复一次
var baseDate = new Date(1900, 0, 1); //创建基准日期对象
var diffDays = Math.floor((date - baseDate) / (1000 * 60 * 60 *24)); //与基准日期相差的天数
var stemIndex = (diffDays +9) %10; //该日对应的天干索引
var branchIndex = (diffDays +11) %12; //该日对应的地支索引
return heavenlyStems[stemIndex] + earthlyBranches[branchIndex]; //返回日柱字符串
}
//根据公历时间计算该时的时柱
function getHourPillar(hour) {
//每个时辰都有固定的地支,从子开始到亥结束,每个时辰占两个小时
var branchIndex = Math.floor(hour /2); //该时对应的地支索引
var branch = earthlyBranches[branchIndex];
var stem;
switch (dayPillar[0]) { //根据全局变量dayPillar的第一个字符判断
case "甲":
case "己":
stem = heavenlyStems[(branchIndex +9) %10]; //从甲开始排列
break;
case "乙":
case "庚":
stem = heavenlyStems[(branchIndex +1) %10]; //从丙开始排列
break;
case "丙":
case "辛":
stem = heavenlyStems[(branchIndex +3) %10]; //从戊开始排列
break;
case "丁":
case "壬":
stem = heavenlyStems[(branchIndex +5) %10]; //从庚开始排列
break;
case "戊":
case "癸":
stem = heavenlyStems[(branchIndex +7) %10]; //从壬开始排列
break;
}
return stem + branch; //返回时柱字符串
}
//年柱、月柱、日柱、时柱
var yearPillar, monthPillar, dayPillar, hourPillar;
//日期字符串(yyyy-mm-dd-hh)
function getBazi(inputDateStr){
//将输入的日期字符串转换为日期对象
var inputDateArr = inputDateStr.split("-");
var inputYear = parseInt(inputDateArr[0]);
var inputMonth= parseInt(inputDateArr[1]);
var inputDay= parseInt(inputDateArr[2]);
var inputHour= parseInt(inputDateArr[3]);
var inputDateObj= new Date(inputYear,inputMonth-1,inputDay,inputHour);
//调用前面定义的函数,计算年柱、月柱、日柱和时柱
yearPillar = getYearPillar(inputYear); //计算年柱
monthPillar = getMonthPillar(inputMonth, false); //计算月柱,暂时不考虑闰月的情况
dayPillar = getDayPillar(inputDateObj); //计算日柱
hourPillar = getHourPillar(inputHour); //计算时柱
//将四个柱子拼接成一个字符串,并返回
var baziStr = yearPillar + " " + monthPillar + " " + dayPillar + " " + hourPillar;
return baziStr;
}
//测试一下
console.log(getBazi("1999-06-26-01"));