js 计算时间差(去除法定节假日,休息日)
作者:互联网
// 法定节假日
var festival = ["2018-12-30", "2018-12-31", "2019-01-01", "2019-02-03", "2019-02-04", "2019-02-05", "2019-02-06", "2019-02-07", "2019-02-08", "2019-02-09",
"2019-02-10", "2019-02-11", "2019-04-05", "2019-04-06", "2019-04-07", "2019-05-01", "2019-05-02", "2019-05-03", "2019-05-04", "2019-06-07", "2019-06-08",
"2019-06-09", "2019-09-13", "2019-09-14", "2019-09-15", "2019-10-01", "2019-10-02", "2019-10-03", "2019-10-04", "2019-10-05", "2019-10-06", "2019-10-07",
"2020-01-01", "2020-01-24", "2020-01-25", "2020-01-26", "2020-01-27", "2020-01-28", "2020-01-29", "2020-01-30", "2020-01-31", "2020-02-01", "2020-02-02",
"2020-04-04", "2020-04-05", "2020-04-06", "2020-05-01", "2020-05-02", "2020-05-03", "2020-05-04", "2020-05-05", "2020-06-25", "2020-06-26", "2020-06-27",
"2020-10-01", "2020-10-02", "2020-10-03", "2020-10-04", "2020-10-05", "2020-10-06", "2020-10-07", "2020-10-08",
"2021-01-01", "2021-02-11", "2021-02-12", "2021-02-13", "2021-02-14", "2021-02-15", "2021-02-16", "2021-02-17", "2021-02-26", "2021-04-02", "2021-04-03",
"2021-04-04", "2021-04-30", "2021-05-01", "2021-05-02", "2021-06-12", "2021-06-13", "2021-06-14", "2021-06-12", "2021-09-19", "2021-09-20", "2021-09-21",
"2021-10-01", "2021-10-02", "2021-10-03", "2021-10-04", "2021-10-05", "2021-10-06", "2021-10-07",
"2022-01-01", "2022-01-31", "2022-02-01", "2022-02-02", "2022-02-03", "2022-02-04", "2022-02-05", "2022-02-06", "2022-04-03", "2022-04-04", "2022-04-05",
"2022-04-30", "2022-05-01", "2022-05-02", "2022-06-03", "2022-06-04", "2022-06-05", "2022-09-09", "2022-09-10", "2022-09-11", "2022-10-01", "2022-10-02",
"2022-10-03", "2022-10-04", "2022-10-05", "2022-10-06", "2022-10-07"
];
//需要上班的周末
var weekendWorkDay = ["2018-12-29", "2019-02-02", "2019-09-29", "2019-10-12", "2019-04-28", "2019-05-05",
"2020-01-19", "2020-04-26", "2020-05-09", "2020-06-28", "2020-09-27", "2020-10-10",
"2021-02-07", "2021-02-20", "2021-09-18", "2021-10-09", "2021-10-10"
];
festival_list = []
weekendWorkDay_list = []
// 将节假日,周末工作日转换为时间戳
festival.forEach((i, item) => {
festival_list.push(new Date(item).valueOf())
})
weekendWorkDay.forEach((i, item) => {
weekendWorkDay_list.push(new Date(item).valueOf())
})
// 过滤俩天之间的周末节假日,返回距离的天数
function formatCountDay(start, end) {
var startday = new Date(start)
var endday = new Date(end)
// 总相差天数
var diffDay = (endday - startday) / (1000 * 60 * 60 * 24) + 1;
if (parseInt(diffDay) === 0)
return parseInt(diffDay);
// 周末周六天数+节假日
var weekEnds = 0;
for (var i = 0; i < diffDay; i++) {
sign = true
// 判断是否是节假日
if (festival_list.includes(startday.valueOf())) {
console.log("节假日");
weekEnds = weekEnds + 1;
sign = false;
}
// 判断周六周日,如果是节假日则不作判断
if (sign === true) {
if (startday.getDay() === 0 || startday.getDay() === 6) {
// 判断该周末是否需要因为节假日补班
weekEnds_sign = 0
for (j in weekendWorkDay_list) {
if (startday.valueOf() === weekendWorkDay_list[j]) {
weekEnds_sign = weekEnds_sign + 1
}
}
if (weekEnds_sign === 0) {
weekEnds = weekEnds + 1;
}
}
}
startday.setDate(startday.getDate() + 1);
}
return parseInt(diffDay - weekEnds)
}
formatCountDay("2021-10-23 23:00:00", "2021-10-25 00:00:00")
标签:02,10,01,时间差,休息日,2020,2021,2019,js 来源: https://blog.csdn.net/KID963931445/article/details/120955570