萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 萬年曆:陰陽曆轉換、生肖、干支、節氣完整算法實現

核心數據表

// 1900-2050 年農歷數據 (每年 16 位二進制:前 12 位月大小,第 13-16 位閏月)
var lunarInfo = [
  0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
  0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
  0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
  0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
  0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
  0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
  0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
  0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
  0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
  0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
  0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
  0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
  0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
  0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
  0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0
];

var Animals = ["鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬"];
var Gan = ["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"];
var Zhi = ["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"];

核心算法類

function Lunar(date) {
  this.date = date;
  this.baseDate = new Date(1900, 0, 31); // 基準日:1900-01-31 (農歷正月初一)
  this.offset = Math.floor((date - this.baseDate) / 86400000); // 相差天數
  
  this.dayCyl = this.offset + 40; // 日柱起始偏移
  this.monCyl = 14;               // 月柱起始偏移
  
  // 計算年份
  var year = 1900;
  for (; year < 2050 && this.offset > 0; year++) {
    var days = this.lYearDays(year);
    this.offset -= days;
    this.monCyl += 12;
  }
  if (this.offset < 0) {
    this.offset += this.lYearDays(--year);
    this.monCyl -= 12;
  }
  this.year = year;
  this.yearCyl = year - 1864; // 甲子年為 1864
  
  // 計算閏月
  this.leapMonth = this.leapMonth(year);
  this.isLeap = false;
  
  // 計算月、日
  for (var m = 1; m <= 12 && this.offset > 0; m++) {
    // 處理閏月
    if (this.leapMonth > 0 && m === this.leapMonth + 1 && !this.isLeap) {
      m--; this.isLeap = true;
      var leapDays = this.leapDays(year);
      this.offset -= leapDays;
      if (this.offset < 0) break;
    } else {
      this.offset -= this.monthDays(year, m);
    }
    if (!this.isLeap) this.monCyl++;
  }
  
  // 修正閏月邊界
  if (this.offset === 0 && this.leapMonth > 0 && m === this.leapMonth + 1) {
    if (this.isLeap) { this.isLeap = false; }
    else { this.isLeap = true; m--; this.monCyl--; }
  }
  if (this.offset < 0) { this.offset += this.monthDays(year, m); m--; this.monCyl--; }
  
  this.month = m;
  this.day = this.offset + 1;
}

// 年總天數
Lunar.prototype.lYearDays = function(y) {
  var sum = 348; // 12*29
  for (var i = 0x8000; i > 0x8; i >>= 1) sum += (lunarInfo[y-1900] & i) ? 1 : 0;
  return sum + this.leapDays(y);
};

// 閏月天數
Lunar.prototype.leapDays = function(y) {
  return this.leapMonth(y) ? ((lunarInfo[y-1900] & 0x10000) ? 30 : 29) : 0;
};

// 閏哪個月 (0=無)
Lunar.prototype.leapMonth = function(y) {
  return lunarInfo[y-1900] & 0xf;
};

// 月天數
Lunar.prototype.monthDays = function(y, m) {
  return (lunarInfo[y-1900] & (0x10000 >> m)) ? 30 : 29;
};

// 干支計算
function cyclical(num) {
  return Gan[num % 10] + Zhi[num % 12];
}

格式化輸出函數

// 陽歷:2024年1月15日 星期一
function formatSolar(date) {
  var week = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
  return date.getFullYear() + "年" + (date.getMonth()+1) + "月" + date.getDate() + "日 " + week[date.getDay()];
}

// 陰歷:甲辰年 臘月初五
function formatLunar(lunar) {
  var yearStr = cyclical(lunar.year - 1900 + 36) + "年"; // 1900是甲子年(36)
  var monthStr = (lunar.isLeap ? "閏" : "") + 
    ["正","二","三","四","五","六","七","八","九","十","冬","臘"][lunar.month-1] + "月";
  var dayStr = ["初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
    "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
    "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"][lunar.day-1];
  return yearStr + monthStr + dayStr;
}

// 干支時辰 (需傳入時辰 0-23)
function getTimeGanZhi(date, lunar) {
  var hour = date.getHours();
  var hourZhi = Math.floor((hour + 1) / 2) % 12; // 子時23-1
  var dayGan = lunar.dayCyl % 10;
  var hourGan = (dayGan * 2 + hourZhi) % 10; // 五虎遁
  return Gan[hourGan] + Zhi[hourZhi];
}

完整頁面演示

<body onload="showCalendar()">
<div id="calendar" style="font-family:SimSun; font-size:14px; line-height:2;"></div>
<script>
function showCalendar() {
  var now = new Date();
  var lunar = new Lunar(now);
  
  var html = '<strong>陽歷:</strong> ' + formatSolar(now) + '<br>';
  html += '<strong>陰歷:</strong> ' + formatLunar(lunar) + '<br>';
  html += '<strong>生肖:</strong> ' + Animals[(now.getFullYear()-4)%12] + '<br>';
  html += '<strong>年干支:</strong> ' + cyclical(lunar.yearCyl) + '<br>';
  html += '<strong>月干支:</strong> ' + cyclical(lunar.monCyl) + '<br>';
  html += '<strong>日干支:</strong> ' + cyclical(lunar.dayCyl) + '<br>';
  html += '<strong>時干支:</strong> ' + getTimeGanZhi(now, lunar) + '<br>';
  
  document.getElementById('calendar').innerHTML = html;
}
</script>

應用場景

  • 日曆組件、黃曆查詢、生日提醒(農歷生日轉陽歷)。
  • 節氣計算:需結合 `sTermInfo` 表(節氣 UTC 時間),本文代碼截斷,完整版可搜索「JS 萬年曆 節氣」。
  • 注意:算法基於 1900-2050 年數據,超出範圍需擴展表。
copyright © 萬盛學電腦網 all rights reserved