欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

工作日休息日统计函数

程序员文章站 2022-05-18 17:08:17
...
/**
* @Title    工作日休息日统计函数
* @charset UTF-8
* @Author  hy([email protected])
* @blog    www.hollowman.cn 
* @Time    2020-04
**/

//返回两个日期之间的天数,两个日期相同表示1天,下同
function betweenDates(date1, date2) {
    var firstDate = new Date(date1);
    var lastDate = new Date(date2);
    return Math.abs((lastDate - firstDate) / (1000 * 3600 * 24)) + 1;
}

//返回两个日期之间的工作日天数(不能判断节假日),休息日天数=betweenDates(date1, date2)-betweenWorkDates(date1, date2)
function betweenWorkDates(date1, date2) {
    var firstDate = new Date(date1) < new Date(date2) ? new Date(date1) : new Date(date2);
    var lastDate = new Date(date1) < new Date(date2) ? new Date(date2) : new Date(date1);
    var dates = (lastDate - firstDate) / (1000 * 3600 * 24) + 1;//2个日期之间的天数
    var k = 0;
    for (var i = 0; i < dates; i++) {
        if (firstDate.getDay() == 0 || firstDate.getDay() == 6 || dateFormat(firstDate) in holidays()) {
            console.log(dateFormat(firstDate) + '休息日');
            console.log(dateFormat(firstDate) in holidays());
            firstDate.setDate(firstDate.getDate() + 1);
        } else {
            console.log(dateFormat(firstDate) + '工作日');
            console.log(dateFormat(firstDate) in holidays());
            firstDate.setDate(firstDate.getDate() + 1);
            k = k + 1;
        }
    }
    return k;
}

//返回两个日期之间的工作日天数,休息日天数=betweenDates(date1, date2)-betweenWorkDates(date1, date2)
function betweenWorkDates1(date1, date2) {
    var firstDate = new Date(date1) < new Date(date2) ? new Date(date1) : new Date(date2);
    var lastDate = new Date(date1) < new Date(date2) ? new Date(date2) : new Date(date1);
    var dates = (lastDate - firstDate) / (1000 * 3600 * 24) + 1;//2个日期之间的天数
    var n = 0;
    for (var i = 0; i < dates; i++) {
        if (firstDate.getDay() == 0 || firstDate.getDay() == 6) {//如果是周六周日
            var arr1 = workdays();
            for (var j = 0; j < arr1.length; j++) {
                if (dateFormat(firstDate) == arr1[j]) {//如果是工作日
                    console.log('双休日加班');        
                    n = n + 1;          
                    break;
                }
            }
            console.log(dateFormat(firstDate));
            console.log(n);
            firstDate.setDate(firstDate.getDate() + 1);
        } else {//如果不是周六周日
            var arr2 = holidays();
            for (var k = 0; k < arr2.length; k++) {
                if (dateFormat(firstDate) == arr2[k]) {//如果是节假日
                    console.log('工作日休息');                  
                    n = n - 1;//n先减去1,退出循环后再加上1,n保持不变,也就是不增加工作日天数
                    break;
                }
            }
            n = n + 1;
            console.log(dateFormat(firstDate));
            console.log(n);
            firstDate.setDate(firstDate.getDate() + 1);
        }
    }
    return n;
}
//返回当前日期之后的n天是那一天
function behindDate(date1, n) {
    var theDate = new Date(date1);
    console.log(theDate.toDateString());
    theDate.setDate(theDate.getDate() + n);
    return theDate.getFullYear() + '/' + (theDate.getMonth() + 1) + '/' + theDate.getDate();
}

//格式化日期为字符串
function dateFormat(date, spli = '') {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    if (m < 10) {
        m = '0' + m;
    }
    if (d < 10) {
        d = '0' + d;
    }
    return y + spli + m + spli + d;
}

//在此函数中加入节假日
function holidays() {
    return [ '20200101','20200124','20200125','20200126','20200127',
    '20200128','20200129','20200130','20200131','20200201',
    '20200202','20200404','20200501','20200502','20200503',
    '20200504','20200505','20200625','20200626','20200627',
    '20201001','20201002','20201003','20201004','20201005',
    '20201006','20201007','20201008'];
}
//次函数中加入调休工作日
function workdays() {
    return ['20200119','20200426', '20200509','20200628','20200927','20201010'];
}