【时间操作 —— getHolidaysInRange】
功能描述: 获取指定日期范围内的所有节假日
1. 函数引入
js
import { getHolidaysInRange } from 'tj-jstools'
1
2. 函数声明
ts
declare const getHolidaysInRange: (startDate: string, endDate: string) => Array<string>;
1
3. 使用示例
3.1 获取指定日期范围内的节假日
ts
const holidays = getHolidaysInRange('2023-01-01', '2023-12-31');
console.log(holidays);
// 输出: ["2023-01-01", "2023-01-22", "2023-01-23", ...]
1
2
3
2
3
3.2 处理跨年日期范围
ts
const holidays = getHolidaysInRange('2022-12-25', '2023-01-05');
console.log(holidays);
// 输出: ["2022-12-25", "2023-01-01"]
1
2
3
2
3
3.3 处理无节假日的日期范围
ts
const holidays = getHolidaysInRange('2023-02-01', '2023-02-28');
console.log(holidays);
// 输出: []
1
2
3
2
3
4. 参数说明
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
startDate | string | 是 | 开始日期,格式为YYYY-MM-DD |
endDate | string | 是 | 结束日期,格式为YYYY-MM-DD |
5. 返回值
返回一个Array<string>
,包含指定日期范围内的所有节假日日期,格式为YYYY-MM-DD
。
6. 注意事项
- 输入的日期字符串必须符合ISO 8601格式
- 节假日数据基于中国的法定节假日安排
- 如果输入的日期格式不正确,将返回空数组
- 支持跨年日期范围查询
- 返回的日期数组已按时间顺序排序
TIP
可以使用dayjs
库对输入的日期进行格式化后再传入该函数