美文网首页
9.JS计算俩个日期之间相差的天数,过滤节假日和周末

9.JS计算俩个日期之间相差的天数,过滤节假日和周末

作者: 陈情令 | 来源:发表于2021-11-05 16:35 被阅读0次

    本来想着找下有没有节假日的插件,可是找不到。所以,我就手写节假日了。

    如果对路过的你有帮助,请随手点个赞。

    // 法定节假日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"];//需要上班的周末varweekendWorkDay=["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=[]// 将节假日,周末工作日转换为时间戳$.each(festival,function(i,item){festival_list.push(newDate(item).valueOf())})$.each(weekendWorkDay,function(i,item){weekendWorkDay_list.push(newDate(item).valueOf())})// 过滤俩天之间的周末节假日,返回距离的天数functionformatCountDay(row){varstartday=newDate(row.work_start_day)varendday=newDate(row.work_end_day)// 总相差天数vardiffDay=(endday-startday)/(1000*60*60*24)+1;if(parseInt(diffDay)===0)returnparseInt(diffDay);// 周末周六天数+节假日varweekEnds=0;for(vari=0;i<diffDay;i++){sign=true// 判断是否是节假日for(kinfestival_list){if(startday.valueOf()===festival_list[k]){weekEnds=weekEnds+1sign=false}}// 判断周六周日,如果是节假日则不作判断if(sign===true){if(startday.getDay()===0||startday.getDay()===6){// 判断该周末是否需要因为节假日补班weekEnds_sign=0for(jinweekendWorkDay_list){if(startday.valueOf()===weekendWorkDay_list[j]){weekEnds_sign=weekEnds_sign+1}}if(weekEnds_sign===0){weekEnds=weekEnds+1}}}startday=startday.valueOf();startday+=1000*60*60*24;startday=newDate(startday);}returnparseInt(diffDay-weekEnds);}

    参考地址:https://blog.csdn.net/myworld_cxc/article/details/107678415?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-5.no_search_link&spm=1001.2101.3001.4242.4

    相关文章

      网友评论

          本文标题:9.JS计算俩个日期之间相差的天数,过滤节假日和周末

          本文链接:https://www.haomeiwen.com/subject/xaykzltx.html