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.
144 lines
3.6 KiB
144 lines
3.6 KiB
|
1 month ago
|
/**
|
||
|
|
* System Date Skill
|
||
|
|
* 获取当前日期和时间(支持时区)
|
||
|
|
*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取当前日期时间
|
||
|
|
* @param {string} timezone - 时区 (默认 Asia/Shanghai)
|
||
|
|
* @returns {Object} 日期时间信息
|
||
|
|
*/
|
||
|
|
function getCurrentDateTime(timezone = 'Asia/Shanghai') {
|
||
|
|
const now = new Date();
|
||
|
|
|
||
|
|
// 转换为指定时区
|
||
|
|
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;
|
||
|
|
});
|
||
|
|
|
||
|
|
// 计算农历日期(基于 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}`
|
||
|
|
: '农历日期需查询黄历';
|
||
|
|
|
||
|
|
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()
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取相对日期
|
||
|
|
* @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);
|
||
|
|
|
||
|
|
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 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;
|
||
|
|
});
|
||
|
|
|
||
|
|
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()
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 格式化日期为可读文本
|
||
|
|
*/
|
||
|
|
function formatDateInfo(dateInfo, includeLunar = true) {
|
||
|
|
const lines = [
|
||
|
|
`📅 **${dateInfo.fullDate}**`,
|
||
|
|
`**星期:** ${dateInfo.weekday}`,
|
||
|
|
];
|
||
|
|
|
||
|
|
if (includeLunar && dateInfo.lunarDate) {
|
||
|
|
lines.push(`**农历:** ${dateInfo.lunarDate}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (dateInfo.timezone) {
|
||
|
|
lines.push(`**时区:** ${dateInfo.timezone}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return lines.join('\n');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 命令行测试
|
||
|
|
if (require.main === module) {
|
||
|
|
const arg = process.argv[2] || 'today';
|
||
|
|
const result = getRelativeDate(arg, 'Asia/Shanghai');
|
||
|
|
console.log(formatDateInfo(result));
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = { getCurrentDateTime, getRelativeDate, formatDateInfo };
|