fix: 农历日期计算 - 北京时间 UTC+8,2/24=初八,2/25=初九

master
Eason (陈医生) 1 month ago
parent 7a59ef08a5
commit f4d300a0c4
  1. 145
      skills/system-date/date.js

@ -1,67 +1,65 @@
/**
* System Date Skill
* 获取当前日期和时间支持时区
* 获取当前日期和时间北京时间 UTC+8
*/
/**
* 获取当前日期时间
* @param {string} timezone - 时区 (默认 Asia/Shanghai)
* @returns {Object} 日期时间信息
* 获取北京时间
*/
function getCurrentDateTime(timezone = 'Asia/Shanghai') {
function getBeijingTime() {
const now = new Date();
// UTC 时间 + 8 小时 = 北京时间
const beijingTime = new Date(now.getTime() + (8 * 60 * 60 * 1000));
return beijingTime;
}
/**
* 计算农历日期2026 年春节 2 17 = 正月初一
*/
function getLunarDate(beijingDate) {
const springFestival = new Date('2026-02-17T00:00:00'); // 春节北京时间
const diffMs = beijingDate.getTime() - springFestival.getTime();
const daysSince = Math.floor(diffMs / (1000 * 60 * 60 * 24));
const lunarDay = daysSince + 1; // 正月初一是第 1 天
// 转换为指定时区
const options = {
timeZone: timezone,
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
hour: '2-digit',
minute: '2-digit',
hour12: false
};
const formatter = new Intl.DateTimeFormat('zh-CN', options);
const parts = formatter.formatToParts(now);
const result = {};
parts.forEach(part => {
result[part.type] = part.value;
});
if (lunarDay >= 1 && lunarDay <= 30) {
return `农历正月初${lunarDay}`;
} else {
return `农历${lunarDay > 30 ? '二月' : '腊月'}${Math.abs(lunarDay - 30)}`;
}
}
/**
* 获取当前日期时间北京时间
*/
function getCurrentDateTime() {
const beijingNow = getBeijingTime();
// 计算农历日期(基于 2026 年春节 2 月 17 日)
const springFestival = new Date('2026-02-17');
const daysSince = Math.floor((now - springFestival) / (1000 * 60 * 60 * 24)) + 1;
const lunarDate = daysSince > 0 && daysSince <= 30
? `农历正月初${daysSince}`
: '农历日期需查询黄历';
const year = beijingNow.getFullYear();
const month = beijingNow.getMonth() + 1;
const day = beijingNow.getDate();
const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
const weekday = `星期${weekdays[beijingNow.getDay()]}`;
return {
success: true,
timezone: timezone,
year: result.year,
month: result.month,
day: result.day,
weekday: result.weekday,
hour: result.hour,
minute: result.minute,
fullDate: `${result.year}${parseInt(result.month)}${parseInt(result.day)}`,
lunarDate: lunarDate,
isoString: now.toISOString()
timezone: 'Asia/Shanghai',
year: year,
month: month,
day: day,
weekday: weekday,
fullDate: `${year}${month}${day}`,
lunarDate: getLunarDate(beijingNow),
isoString: beijingNow.toISOString()
};
}
/**
* 获取相对日期
* @param {string} relative - 相对日期 (today, tomorrow, yesterday)
* @param {string} timezone - 时区
* @returns {Object} 日期信息
* 获取相对日期北京时间
*/
function getRelativeDate(relative = 'today', timezone = 'Asia/Shanghai') {
const now = new Date();
let targetDate = new Date(now);
function getRelativeDate(relative = 'today') {
const beijingNow = getBeijingTime();
let targetDate = new Date(beijingNow);
switch (relative.toLowerCase()) {
case 'today':
@ -77,39 +75,22 @@ function getRelativeDate(relative = 'today', timezone = 'Asia/Shanghai') {
break;
}
// 计算农历日期
const springFestival = new Date('2026-02-17');
const daysSince = Math.floor((targetDate - springFestival) / (1000 * 60 * 60 * 24)) + 1;
const lunarDate = daysSince > 0 && daysSince <= 30
? `农历正月初${daysSince}`
: '农历日期需查询黄历';
const options = {
timeZone: timezone,
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long'
};
const formatter = new Intl.DateTimeFormat('zh-CN', options);
const parts = formatter.formatToParts(targetDate);
const result = {};
parts.forEach(part => {
result[part.type] = part.value;
});
const year = targetDate.getFullYear();
const month = targetDate.getMonth() + 1;
const day = targetDate.getDate();
const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
const weekday = `星期${weekdays[targetDate.getDay()]}`;
return {
success: true,
relative: relative,
year: result.year,
month: result.month,
day: result.day,
weekday: result.weekday,
fullDate: `${result.year}${parseInt(result.month)}${parseInt(result.day)}`,
lunarDate: lunarDate,
isoString: targetDate.toISOString()
year: year,
month: month,
day: day,
weekday: weekday,
fullDate: `${year}${month}${day}`,
lunarDate: getLunarDate(targetDate),
timezone: 'Asia/Shanghai'
};
}
@ -126,18 +107,20 @@ function formatDateInfo(dateInfo, includeLunar = true) {
lines.push(`**农历:** ${dateInfo.lunarDate}`);
}
if (dateInfo.timezone) {
lines.push(`**时区:** ${dateInfo.timezone}`);
}
lines.push(`**时区:** ${dateInfo.timezone || 'Asia/Shanghai'}`);
return lines.join('\n');
}
// 命令行测试
if (require.main === module) {
console.log('=== 北京时间测试 ===');
console.log('今天:', new Date(new Date().getTime() + 8*60*60*1000).toISOString());
console.log('');
const arg = process.argv[2] || 'today';
const result = getRelativeDate(arg, 'Asia/Shanghai');
const result = getRelativeDate(arg);
console.log(formatDateInfo(result));
}
module.exports = { getCurrentDateTime, getRelativeDate, formatDateInfo };
module.exports = { getCurrentDateTime, getRelativeDate, formatDateInfo, getLunarDate, getBeijingTime };

Loading…
Cancel
Save