初始化
This commit is contained in:
119
src/utils/calendarUtils.js
Normal file
119
src/utils/calendarUtils.js
Normal file
@@ -0,0 +1,119 @@
|
||||
import dayjs from 'dayjs';
|
||||
import * as chineseLunar from 'chinese-lunar';
|
||||
|
||||
/**
|
||||
* 获取农历日期
|
||||
* @param {string} date - YYYY-MM-DD格式的日期
|
||||
* @returns {string} 农历日期(初一显示月份)
|
||||
*/
|
||||
export function getLunarDate(date) {
|
||||
try {
|
||||
// 使用chinese-lunar库获取农历信息
|
||||
const lunar = chineseLunar.solarToLunar(new Date(date));
|
||||
|
||||
const month = chineseLunar.format(lunar, 'M');
|
||||
const day = chineseLunar.format(lunar, 'D');
|
||||
|
||||
console.log(date, chineseLunar.dayName(lunar))
|
||||
// console.log(date, lunar, month + day)
|
||||
if (day === '初一') {
|
||||
return month;
|
||||
} else {
|
||||
return day;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取农历日期错误:', error);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节假日信息
|
||||
* @param {string} date - YYYY-MM-DD格式的日期
|
||||
* @returns {string|null} 节假日名称(如果没有则为null)
|
||||
*/
|
||||
export function getFestival(date) {
|
||||
try {
|
||||
return '';
|
||||
// 使用dayjs解析日期
|
||||
const d = dayjs(date);
|
||||
const month = d.month() + 1;
|
||||
const day = d.date();
|
||||
|
||||
// 公历节日
|
||||
if (month === 1 && day === 1) return '元旦';
|
||||
if (month === 5 && day === 1) return '劳动节';
|
||||
if (month === 10 && day === 1) return '国庆节';
|
||||
|
||||
// 农历节日 - 使用chinese-lunar获取农历日期
|
||||
const year = d.year();
|
||||
const lunar = chineseLunar.solarToLunar(new Date(date));
|
||||
|
||||
// 农历节日
|
||||
if (lunar.lunarMonth === 1 && lunar.lunarDay === 1) return '春节';
|
||||
if (lunar.lunarMonth === 1 && lunar.lunarDay === 15) return '元宵节';
|
||||
if (lunar.lunarMonth === 5 && lunar.lunarDay === 5) return '端午节';
|
||||
if (lunar.lunarMonth === 8 && lunar.lunarDay === 15) return '中秋节';
|
||||
if (lunar.lunarMonth === 9 && lunar.lunarDay === 9) return '重阳节';
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('获取节日错误:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节气信息
|
||||
* @param {string} date - YYYY-MM-DD格式的日期
|
||||
* @returns {string|null} 节气名称(如果没有则为null)
|
||||
*/
|
||||
export function getSolarTerm(date) {
|
||||
return '大雪';
|
||||
const d = dayjs(date);
|
||||
const month = d.month() + 1;
|
||||
const day = d.date();
|
||||
|
||||
// 24节气(简化版)
|
||||
const terms = [
|
||||
// 每个月的两个节气
|
||||
[1, 5, '小寒'], [1, 20, '大寒'],
|
||||
[2, 4, '立春'], [2, 19, '雨水'],
|
||||
[3, 5, '惊蛰'], [3, 20, '春分'],
|
||||
[4, 5, '清明'], [4, 20, '谷雨'],
|
||||
[5, 5, '立夏'], [5, 21, '小满'],
|
||||
[6, 6, '芒种'], [6, 21, '夏至'],
|
||||
[7, 7, '小暑'], [7, 23, '大暑'],
|
||||
[8, 7, '立秋'], [8, 23, '处暑'],
|
||||
[9, 8, '白露'], [9, 23, '秋分'],
|
||||
[10, 8, '寒露'], [10, 23, '霜降'],
|
||||
[11, 7, '立冬'], [11, 22, '小雪'],
|
||||
[12, 7, '大雪'], [12, 22, '冬至']
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为法定假日
|
||||
* @param {string} date - YYYY-MM-DD格式的日期
|
||||
* @returns {boolean} 是否为法定假日
|
||||
*/
|
||||
export function isHoliday(date) {
|
||||
try {
|
||||
// 使用dayjs解析日期
|
||||
const d = dayjs(date);
|
||||
const dayOfWeek = d.day();
|
||||
|
||||
// 周六或周日为休息日
|
||||
if (dayOfWeek === 0 || dayOfWeek === 6) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 检查是否为法定节假日
|
||||
const festival = getFestival(date);
|
||||
return !!festival;
|
||||
} catch (error) {
|
||||
console.error('判断假日错误:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
0
src/utils/utils.js
Normal file
0
src/utils/utils.js
Normal file
Reference in New Issue
Block a user