js 两个时间之间工作日的计算问题(包含节假日)

后端用vue写点前端代码,也是罪过。。。嗷嗷嗷  行吧

项目需求描述:需要对时间控件里面的两个时间计算出相隔多少个工作日(包含节假日),由于节假日每年都不确定。保证项目能够正常使用,录用了2018年和2019年的所有节假日期(暂时只能使用笨办法了)。如图,看大致效果:

                              

    这里实际业务需要,申请收到时间答复时间必须相差15个工作日以内,提示正常。如果超过15个工作日就提示错误,见图:

2019年3月2号和2019年3月3号是节假日,所以这里选择3月4号。超过实际业务需要,提示错误信息。

 

接下来上代码:

 

//录用2018年和2019年所有节假日期  单独取个文件名称 holiday.js

export const HOLIDAY = ["2018/01/01", "2018/01/06", "2018/01/07", "2018/01/13", "2018/01/14", "2018/01/20", "2018/01/21", "2018/01/27", "2018/01/28", "2018/02/03", "2018/02/04", "2018/02/10", "2018/02/15", "2018/02/16", "2018/02/17", "2018/02/18", "2018/02/19", "2018/02/20", "2018/02/21", "2018/02/25", "2018/03/03", "2018/03/04", "2018/03/10", "2018/03/11", "2018/03/17", "2018/03/18", "2018/03/24", "2018/03/25", "2018/03/31", "2018/04/01", "2018/04/05", "2018/04/06", "2018/04/07", "2018/04/14", "2018/04/15", "2018/04/21", "2018/04/22", "2018/04/29", "2018/04/30", "2018/05/01", "2018/05/05", "2018/05/06", "2018/05/12", "2018/05/13", "2018/05/19", "2018/05/20", "2018/05/26", "2018/05/27", "2018/06/02", "2018/06/03", "2018/06/09", "2018/06/10", "2018/06/16", "2018/06/17", "2018/06/18", "2018/06/23", "2018/06/24", "2018/06/30", "2018/07/01", "2018/07/07", "2018/07/08", "2018/07/14", "2018/07/15", "2018/07/21", "2018/07/22", "2018/07/28", "2018/07/29", "2018/08/04", "2018/08/05", "2018/08/11", "2018/08/12", "2018/08/18", "2018/08/19", "2018/08/25", "2018/08/26", "2018/09/01", "2018/09/02", "2018/09/08", "2018/09/09", "2018/09/15", "2018/09/16", "2018/09/22", "2018/09/23", "2018/09/24", "2018/10/01", "2018/10/02", "2018/10/03", "2018/10/04", "2018/10/05", "2018/10/06", "2018/10/07", "2018/10/13", "2018/10/14", "2018/10/20", "2018/10/21", "2018/10/27", "2018/10/28", "2018/11/03", "2018/11/04", "2018/11/10", "2018/11/11", "2018/11/17", "2018/11/18", "2018/11/24", "2018/11/25", "2018/12/01", "2018/12/02", "2018/12/08", "2018/12/09", "2018/12/15", "2018/12/16", "2018/12/22", "2018/12/23", "2018/12/30", "2018/12/31", "2019/01/01", "2019/01/05", "2019/01/06", "2019/01/12", "2019/01/13", "2019/01/19", "2019/01/20", "2019/01/26", "2019/01/27", "2019/02/04", "2019/02/05", "2019/02/06", "2019/02/07", "2019/02/08", "2019/02/09", "2019/02/10", "2019/02/16", "2019/02/17", "2019/02/23", "2019/02/24", "2019/03/02", "2019/03/03", "2019/03/09", "2019/03/10", "2019/03/16", "2019/03/17", "2019/03/23", "2019/03/24", "2019/03/30", "2019/03/31", "2019/04/05", "2019/04/06", "2019/04/07", "2019/04/13", "2019/04/14", "2019/04/20", "2019/04/21", "2019/04/27", "2019/04/28", "2019/05/01", "2019/05/04", "2019/05/05", "2019/05/11", "2019/05/12", "2019/05/18", "2019/05/19", "2019/05/25", "2019/05/26", "2019/06/01", "2019/06/02", "2019/06/07", "2019/06/08", "2019/06/09", "2019/06/15", "2019/06/16", "2019/06/22", "2019/06/23", "2019/06/29", "2019/06/30", "2019/07/06", "2019/07/07", "2019/07/13", "2019/07/14", "2019/07/20", "2019/07/21", "2019/07/27", "2019/07/28", "2019/08/03", "2019/08/04", "2019/08/10", "2019/08/11", "2019/08/17", "2019/08/18", "2019/08/24", "2019/08/25", "2019/08/31", "2019/09/01", "2019/09/07", "2019/09/08", "2019/09/13", "2019/09/14", "2019/09/15", "2019/09/21", "2019/09/22", "2019/09/28", "2019/10/01", "2019/10/02", "2019/10/03", "2019/10/04", "2019/10/05", "2019/10/06", "2019/10/07", "2019/10/13", "2019/10/19", "2019/10/20", "2019/10/26", "2019/10/27", "2019/11/02", "2019/11/03", "2019/11/09", "2019/11/10", "2019/11/16", "2019/11/17", "2019/11/23", "2019/11/24", "2019/11/30", "2019/12/01", "2019/12/07", "2019/12/08", "2019/12/14", "2019/12/15", "2019/12/21", "2019/12/22", "2019/12/28", "2019/12/29"];

 

import {HOLIDAY} from '../../constant/holiday.js'  ;         //导入节假日

  countWorkDay:function(startTime,endTime){

        if(startTime === undefined) {

          startTime = new Date();

        } else {

          startTime = startTime.replace('年','/').replace('月','/').replace('日','');  //将时间格式转化为2019/02/10的格式

        }

        if(endTime === undefined) {

          endTime = new Date();

        } else {

          endTime = endTime.replace('年','/').replace('月','/').replace('日','');  //将时间格式转化为2019/02/10的格式

        }

        var start_time = new Date(startTime);

        var end_time = new Date(endTime);

        var time= end_time.getTime() - start_time.getTime();      //计算两个时间的毫秒差

        var days = parseInt(time / (1000 * 60 * 60 * 24));             //根据毫秒差计算得到相差天数

        if(days < 0){                                                                      //实际业务需要,某个时间不能小于某个时间,否则给出错误提示

          return days;                                

        }

        days+=1;                                                                          // 实际业务需要计算天数(当天也算一天)

        for (var i = 0; i < HOLIDAY.length; i++) {                            //获取两个时间之间的工作日天数

          var holidaytime = new Date(HOLIDAY[i]).getTime();

          if (holidaytime >= start_time.getTime() && holidaytime <= end_time.getTime()) {    //如果两个时间之间包含节假日期就减去那天,得到需要的工作日天数

            days -= 1;

          }

        }

        return days;                           

      }

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页