节假日

背景

工时统计,经常需要

①减除国家法定节假日

②并增加周末上班时长

这些节假日安排,是由国务院制定,并在上年末(通常是上年11月份)公布在中国政府网。例如《国务院办公厅关于2021年部分节假日安排的通知》

而后,第三方的万年历网站会对新年节假日情况进行手动引入,基于此,万年历只能有当年及当年之前的节假日情况。如图示例,目前2021年,则2022年没有节假日信息。

方法

基于此,为了获取节假日可以采用两种手段

  1. 等待政府消息,手动维护入库
  2. 借助已经处理过的第三方万年历入库

第三方万年历网站:

链接 描述
中华万年历 http://yun.rili.cn/wnl/index.html 提供json返回数据,易于直接获取
百度 http://opendata.baidu.com/api.php?query=2020%E5%B9%B45%E6%9C%88&resource_id=6018&format=json 【老旧】经过尝试,无法获取最新年份的节假日
聚合数据 https://www.juhe.cn/search/%E4%B8%87%E5%B9%B4%E5%8E%86 【麻烦】需要注册登录身份证实名认证
其他个人

以中华万年历为例——

部署访问: http://10.139.18.182:9923/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
async onClickToGetAll() {
const year = "2021";

// 中华万年历 http://yun.rili.cn/wnl/index.html
const url = "http://pc.suishenyun.net/peacock/api/h5/festival";

const result = await axios.get(url);

const list = result.data.holidays.cn;
const yearList = list.filter((item) =>
item.date.toString().startsWith(year)
);

// 放假
const holiDayList = yearList
.filter((item) => item.status == 0)
.map((item) => item.date);
// 周末上班
const workDayList = yearList
.filter((item) => item.status == 1)
.map((item) => item.date);

// 工作日放假(抛去周末放假(周末本来就是节假日嘛))
const weekendList = this.getWeekendList(year);
const holiDayListExceptWeekend = holiDayList.filter(
(item) => !weekendList.includes(item)
);

console.log(workDayList);
console.log(holiDayListExceptWeekend);
},

// 待优化
getWeekendList(year) {
const firstYearDate = moment(`${year}-01-01`);

let firstYearWeek;
for (let i = 0; i < 7; i++) {
if (
0 ==
moment(firstYearDate)
.add(i, "days")
.weekday()
) {
firstYearWeek = moment(firstYearDate).add(i, "days");
break;
}
}

const list = [];
while (firstYearWeek.year() == year) {
if (
moment(firstYearWeek)
.add(-1, "days")
.year() == year
) {
list.push(firstYearWeek.clone().add(-1, "days"));
}
list.push(firstYearWeek.clone());
firstYearWeek = moment(firstYearWeek).add(7, "days");
}
return list.map((item) => parseInt(item.format("YYYYMMDD")));
},

参考

聚合数据使用案例:

https://www.jianshu.com/p/ef14da9b6b6a

-------------Keep It Simple Stupid-------------
0%