JS端计算一段时间内工作日的天数,排除周末和法定节假日,同时考虑到调休日

//法定节假日和调休日的设定
var Holiday = [“2012-01-01”, “2012-01-02”, “2012-01-03”, “2012-01-22”, “2012-01-23”, “2012-01-24”, “2012-01-25”, “2012-01-26”, “2012-01-27”, “2012-01-28”, “2012-04-02”, “2012-04-03”, “2012-04-04”, “2012-04-29”, “2012-04-30”, “2012-05-01”, “2012-06-22”, “2012-06-23”, “2012-06-24”, “2012-09-30”, “2012-10-01”, “2012-10-02”, “2012-10-03”, “2012-10-04”, “2012-10-05”, “2012-10-06”, “2012-10-07”];

var WeekendsOff = [“2011-12-31”, “2012-01-21”, “2012-01-29”, “2012-03-31”, “2012-04-01”, “2012-04-28”, “2012-09-29”];

function nearlyWeeks (mode, weekcount, end) {
/*
功能:计算当前时间(或指定时间),向前推算周数(weekcount),得到结果周的第一天的时期值;
参数:
mode -推算模式('cn’表示国人习惯【周一至周日】;'en’表示国际习惯【周日至周一】)
weekcount -表示周数(0-表示本周, 1-前一周,2-前两周,以此推算);
end -指定时间的字符串(未指定则取当前时间);
*/

if (mode == undefined) mode = "cn";
if (weekcount == undefined) weekcount = 0;
if (end != undefined)
    end = new Date(new Date(end).toDateString());
else
    end = new Date(new Date().toDateString());

var days = 0;
if (mode == "cn")
    days = (end.getDay() == 0 ? 7 : end.getDay()) - 1;
else
    days = end.getDay();

return new Date(end.getTime() - (days + weekcount * 7) * 24 * 60 * 60 * 1000);

};

function getWorkDayCount (mode, beginDay, endDay) {
/*
功能:计算一段时间内工作的天数。不包括周末和法定节假日,法定调休日为工作日,周末为周六、周日两天;
参数:
mode -推算模式('cn’表示国人习惯【周一至周日】;'en’表示国际习惯【周日至周一】)
beginDay -时间段开始日期;
endDay -时间段结束日期;
*/
var begin = new Date(beginDay.toDateString());
var end = new Date(endDay.toDateString());

//每天的毫秒总数,用于以下换算
var daytime = 24 * 60 * 60 * 1000;
//两个时间段相隔的总天数
var days = (end - begin) / daytime + 1;
//时间段起始时间所在周的第一天
var beginWeekFirstDay = nearlyWeeks(mode, 0, beginDay.getTime()).getTime();
//时间段结束时间所在周的最后天
var endWeekOverDay = nearlyWeeks(mode, 0, endDay.getTime()).getTime() + 6 * daytime;

//由beginWeekFirstDay和endWeekOverDay换算出,周末的天数
var weekEndCount = ((endWeekOverDay - beginWeekFirstDay) / daytime + 1) / 7 * 2;
//根据参数mode,调整周末天数的值
if (mode == "cn") {
    if (endDay.getDay() > 0 && endDay.getDay() < 6)
        weekEndCount -= 2;
    else if (endDay.getDay() == 6)
        weekEndCount -= 1;

    if (beginDay.getDay() == 0) weekEndCount -= 1;
}
else {
    if (endDay.getDay() < 6) weekEndCount -= 1;

    if (beginDay.getDay() > 0) weekEndCount -= 1;
}

//根据调休设置,调整周末天数(排除调休日)
$.each(WLD.Setting.WeekendsOff, function (i, offitem) {
    var itemDay = new Date(offitem.split('-')[0] + "/" + offitem.split('-')[1] + "/" + offitem.split('-')[2]);
    //如果调休日在时间段区间内,且为周末时间(周六或周日),周末天数值-1
    if (itemDay.getTime() >= begin.getTime() && itemDay.getTime() <= end.getTime() && (itemDay.getDay() == 0 || itemDay.getDay() == 6))
        weekEndCount -= 1;
});
//根据法定假日设置,计算时间段内周末的天数(包含法定假日)
$.each(WLD.Setting.Holiday, function (i, itemHoliday) {
    var itemDay = new Date(itemHoliday.split('-')[0] + "/" + itemHoliday.split('-')[1] + "/" + itemHoliday.split('-')[2]);
    //如果法定假日在时间段区间内,且为工作日时间(周一至周五),周末天数值+1
    if (itemDay.getTime() >= begin.getTime() && itemDay.getTime() <= end.getTime() && itemDay.getDay() > 0 && itemDay.getDay() < 6)
        weekEndCount += 1;
});

//工作日 = 总天数 - 周末天数(包含法定假日并排除调休日)
return days - weekEndCount;

};

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现这个功能需要以下几个步骤: 1. 获取法定节假日列表和调休列表,这些数据可以从后接口或者本地存储中获取。 2. 计算起止期之间的所有期,并将它们存储在一个数组中。 3. 遍历数组,对于每个期,判断它是否是周末或者法定节假日或者调休。如果是,排除它。 4. 最后计算剩余的期数量,即为工作日天数。 下面是一个使用 JavaScript 实现的示例代码: ```javascript // 假设法定节假日调休的数据分别存储在 holidayList 和 adjustedList 中 function getWorkingDays(startDate, endDate, holidayList, adjustedList) { // 计算起止期之间的所有期 const dateList = []; let currentDate = new Date(startDate); while (currentDate <= endDate) { dateList.push(new Date(currentDate)); currentDate.setDate(currentDate.getDate() + 1); } // 排除周末法定节假日调休 let workingDays = 0; dateList.forEach(date => { // 判断是否是周末 if (date.getDay() !== 0 && date.getDay() !== 6) { // 判断是否是法定节假日 const isHoliday = holidayList.some(holiday => { return holiday === date.toISOString().slice(0, 10); }); // 判断是否是调休 const isAdjust = adjustedList.some(adjust => { return adjust === date.toISOString().slice(0, 10); }); if (!isHoliday || isAdjust) { workingDays++; } } }); return workingDays; } // 调用示例 const holidayList = ["2022-01-01", "2022-01-02", "2022-01-03"]; // 假设法定节假日为2022年元旦 const adjustedList = ["2022-01-08"]; // 假设2022年元旦调休到了2022年1月8 const startDate = new Date("2022-01-01"); const endDate = new Date("2022-01-10"); const workingDays = getWorkingDays(startDate, endDate, holidayList, adjustedList); console.log(workingDays); // 输出结果为 3,即2022年1月4、1月5、1月7工作日 ``` 这个示例代码比较简单,实际开发中还需要考虑一些边界情况和优化性能的问题,例如如何提前加载节假日调休数据,如何处理时区等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值