You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.8 KiB
146 lines
3.8 KiB
/** |
|
* System Date Skill |
|
* 获取当前日期和农历(使用 lunar-javascript 库) |
|
*/ |
|
|
|
const { Solar, Lunar } = require('lunar-javascript'); |
|
|
|
/** |
|
* 获取北京时间 |
|
*/ |
|
function getBeijingTime() { |
|
const now = new Date(); |
|
// UTC + 8 = 北京时间 |
|
return new Date(now.getTime() + (8 * 60 * 60 * 1000)); |
|
} |
|
|
|
/** |
|
* 获取农历日期(使用专业库) |
|
*/ |
|
function getLunarInfo(beijingDate) { |
|
// 从公历转换为农历 |
|
const solar = Solar.fromDate(beijingDate); |
|
const lunar = solar.getLunar(); |
|
|
|
return { |
|
lunarDate: lunar.toString(), // 如:二〇二六年正月初八 |
|
lunarDay: lunar.getDayInChinese(), // 如:初八 |
|
lunarMonth: lunar.getMonthInChinese(), // 如:正月 |
|
lunarYear: lunar.getYearInChinese(), // 如:二〇二六 |
|
isLeap: false // lunar-javascript 需要其他方法判断闰月 |
|
}; |
|
} |
|
|
|
/** |
|
* 获取当前日期时间(北京时间) |
|
*/ |
|
function getCurrentDateTime() { |
|
const beijingNow = getBeijingTime(); |
|
|
|
const year = beijingNow.getFullYear(); |
|
const month = beijingNow.getMonth() + 1; |
|
const day = beijingNow.getDate(); |
|
const weekdays = ['日', '一', '二', '三', '四', '五', '六']; |
|
const weekday = `星期${weekdays[beijingNow.getDay()]}`; |
|
|
|
const lunarInfo = getLunarInfo(beijingNow); |
|
|
|
return { |
|
success: true, |
|
timezone: 'Asia/Shanghai', |
|
year: year, |
|
month: month, |
|
day: day, |
|
weekday: weekday, |
|
fullDate: `${year}年${month}月${day}日`, |
|
lunarDate: lunarInfo.lunarDate, |
|
lunarDay: lunarInfo.lunarDay, |
|
lunarMonth: lunarInfo.lunarMonth, |
|
lunarYear: lunarInfo.lunarYear, |
|
isLeap: lunarInfo.isLeap, |
|
isoString: beijingNow.toISOString() |
|
}; |
|
} |
|
|
|
/** |
|
* 获取相对日期(北京时间) |
|
*/ |
|
function getRelativeDate(relative = 'today') { |
|
const beijingNow = getBeijingTime(); |
|
let targetDate = new Date(beijingNow); |
|
|
|
switch (relative.toLowerCase()) { |
|
case 'today': |
|
case '今天': |
|
break; |
|
case 'tomorrow': |
|
case '明天': |
|
targetDate.setDate(targetDate.getDate() + 1); |
|
break; |
|
case 'yesterday': |
|
case '昨天': |
|
targetDate.setDate(targetDate.getDate() - 1); |
|
break; |
|
} |
|
|
|
const year = targetDate.getFullYear(); |
|
const month = targetDate.getMonth() + 1; |
|
const day = targetDate.getDate(); |
|
const weekdays = ['日', '一', '二', '三', '四', '五', '六']; |
|
const weekday = `星期${weekdays[targetDate.getDay()]}`; |
|
|
|
const lunarInfo = getLunarInfo(targetDate); |
|
|
|
return { |
|
success: true, |
|
relative: relative, |
|
year: year, |
|
month: month, |
|
day: day, |
|
weekday: weekday, |
|
fullDate: `${year}年${month}月${day}日`, |
|
lunarDate: lunarInfo.lunarDate, |
|
lunarDay: lunarInfo.lunarDay, |
|
lunarMonth: lunarInfo.lunarMonth, |
|
lunarYear: lunarInfo.lunarYear, |
|
isLeap: lunarInfo.isLeap, |
|
timezone: 'Asia/Shanghai' |
|
}; |
|
} |
|
|
|
/** |
|
* 格式化日期为可读文本 |
|
*/ |
|
function formatDateInfo(dateInfo, includeLunar = true) { |
|
const lines = [ |
|
`📅 **${dateInfo.fullDate}**`, |
|
`**星期:** ${dateInfo.weekday}`, |
|
]; |
|
|
|
if (includeLunar && dateInfo.lunarDate) { |
|
lines.push(`**农历:** ${dateInfo.lunarDate}`); |
|
if (dateInfo.isLeap) { |
|
lines.push(`**闰月:** 是`); |
|
} |
|
} |
|
|
|
lines.push(`**时区:** ${dateInfo.timezone || 'Asia/Shanghai'}`); |
|
|
|
return lines.join('\n'); |
|
} |
|
|
|
// 命令行测试 |
|
if (require.main === module) { |
|
console.log('=== 农历计算测试(使用 lunar-javascript 库)===\n'); |
|
|
|
const arg = process.argv[2] || 'today'; |
|
const result = getRelativeDate(arg); |
|
console.log(formatDateInfo(result)); |
|
console.log('\n详细信息:'); |
|
console.log(`农历年:${result.lunarYear}`); |
|
console.log(`农历月:${result.lunarMonth}`); |
|
console.log(`农历日:${result.lunarDay}`); |
|
console.log(`是否闰月:${result.isLeap ? '是' : '否'}`); |
|
} |
|
|
|
module.exports = { getCurrentDateTime, getRelativeDate, formatDateInfo, getLunarInfo, getBeijingTime };
|
|
|