Initial Commit

This commit is contained in:
yuming
2025-10-26 19:29:30 +08:00
commit 6747ade9c4
33 changed files with 4387 additions and 0 deletions
+130
View File
@@ -0,0 +1,130 @@
/**
* 农历日期转换工具
* 使用简化版的农历计算方法
*/
// 1900-2100年的农历数据
const 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, 0x16a95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950
]
/**
* 农历年份
*/
const lunarMonths = ['正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊']
const lunarDays = ['初', '十', '廿', '三']
const lunarDayNames = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
/**
* 获取农历月份名称
*/
function getLunarMonthName(month) {
return lunarMonths[month - 1]
}
/**
* 获取农历日期名称
*/
function getLunarDayName(day) {
if (day === 10) return '初十'
if (day === 20) return '二十'
if (day === 30) return '三十'
const tens = Math.floor(day / 10)
const units = day % 10
let name = lunarDays[tens]
if (units > 0) {
name += lunarDayNames[units - 1]
}
return name
}
/**
* 计算农历指定年的总天数
*/
function getLunarYearDays(year) {
let total = 0
for (let i = 0; i < 13; i++) {
const days = lunarInfo[year] & (0x8000 >> i) ? 30 : 29
total += days
}
return total
}
/**
* 公历转农历
* @param {Date} solarDate - 公历日期对象
* @returns {Object} 农历日期对象 {year, month, day, lunarText}
*/
function solarToLunar(solarDate) {
const year = solarDate.getFullYear()
const month = solarDate.getMonth() + 1
const day = solarDate.getDate()
// 这是一个简化版本,实际计算需要完整的农历转换算法
// 这里返回一个示例结果
// 实际项目中建议使用成熟的农历库如 lunar-javascript
const offset = Math.floor((year - 1900) / 4) + (year - 1900) % 4
// 返回格式化的农历
return {
year: year,
month: month,
day: day,
lunarText: `${getLunarMonthName(month)}${getLunarDayName(day)}`
}
}
/**
* 农历转公历
* @param {Number} year - 农历年份
* @param {Number} month - 农历月份
* @param {Number} day - 农历日期
* @returns {Date} 公历日期对象
*/
function lunarToSolar(year, month, day) {
// 简化版本,实际需要完整的农历转换算法
// 这里返回当前日期作为示例
// 实际项目中建议使用成熟的农历库
const now = new Date()
return new Date(now.getFullYear(), month - 1, day)
}
/**
* 获取农历年份的下一个相同农历日期的公历日期
* @param {Number} lunarYear - 农历年份
* @param {Number} lunarMonth - 农历月份
* @param {Number} lunarDay - 农历日期
* @returns {Date} 下一次该农历日期的公历日期
*/
function getNextLunarDate(lunarYear, lunarMonth, lunarDay) {
// 简化版本,返回明年同一天的日期
const today = new Date()
const nextYear = today.getFullYear() + 1
return new Date(nextYear, today.getMonth(), today.getDate())
}
/**
* 格式化农历显示文本
*/
function formatLunarText(lunarDate) {
const lunar = solarToLunar(new Date(lunarDate.year, lunarDate.month - 1, lunarDate.day))
return lunar.lunarText
}
module.exports = {
solarToLunar,
lunarToSolar,
getNextLunarDate,
formatLunarText,
getLunarMonthName,
getLunarDayName
}