«

2021年8月5日 JavaScript获取工作日

时间:2021-8-5 17:15     作者:Mahalalel     分类: JavaScript


背景

小伙伴Lucas有个需求:初始时间为工作日,要找n天后的工作日的时间

实现

第一种

    /**
     * 判断下一个工作日的时间
     */
    function get_next_weekday(date) {
        var tomorrow = new Date(date.setDate(date.getDate() + 1))
        return tomorrow.getDay() % 6 ? tomorrow : get_next_weekday(tomorrow)
    }

    function getWeekday(date,num){
        for (let i = 1; i <= num; i++) {
            date = get_next_weekday(date);
            console.log(date)
        }
        console.log("最后:"+date)
        return date;
    }
    getWeekday(new Date(), 10)

日志输出结果:

Fri Aug 06 2021 17:33:52 GMT+0800 (中国标准时间)
Mon Aug 09 2021 17:33:52 GMT+0800 (中国标准时间)
Tue Aug 10 2021 17:33:52 GMT+0800 (中国标准时间)
Wed Aug 11 2021 17:33:52 GMT+0800 (中国标准时间)
Thu Aug 12 2021 17:33:52 GMT+0800 (中国标准时间)
Fri Aug 13 2021 17:33:52 GMT+0800 (中国标准时间)
Mon Aug 16 2021 17:33:52 GMT+0800 (中国标准时间)
Tue Aug 17 2021 17:33:52 GMT+0800 (中国标准时间)
Wed Aug 18 2021 17:33:52 GMT+0800 (中国标准时间)
Thu Aug 19 2021 17:33:52 GMT+0800 (中国标准时间)
最后:Thu Aug 19 2021 17:33:52 GMT+0800 (中国标准时间)

第二种方式

function getWeekday(date, num) {
    for(let i = 1; i <= num; i++) {
        date = new Date(+date);
        do {
            date.setDate(date.getDate() + 1);
        } while (!(date.getDay() % 6))
        console.log(date)
    }
    console.log("最后:"+date)
}
getWeekday(new Date(), 10)

日志输出结果:

Fri Aug 06 2021 17:35:41 GMT+0800 (中国标准时间)
Mon Aug 09 2021 17:35:41 GMT+0800 (中国标准时间)
Tue Aug 10 2021 17:35:41 GMT+0800 (中国标准时间)
Wed Aug 11 2021 17:35:41 GMT+0800 (中国标准时间)
Thu Aug 12 2021 17:35:41 GMT+0800 (中国标准时间)
Fri Aug 13 2021 17:35:41 GMT+0800 (中国标准时间)
Mon Aug 16 2021 17:35:41 GMT+0800 (中国标准时间)
Tue Aug 17 2021 17:35:41 GMT+0800 (中国标准时间)
Wed Aug 18 2021 17:35:41 GMT+0800 (中国标准时间)
Thu Aug 19 2021 17:35:41 GMT+0800 (中国标准时间)
最后:Thu Aug 19 2021 17:35:41 GMT+0800 (中国标准时间)

第三种方式

function getWorkDate(startDate, limitDay) {
    var startTime = startDate.getTime();
    var T = 24 * 60 * 60 * 1000;
    var endTime = startTime + (limitDay * T);
    if(limitDay > 0) {
        var holidays = 0;
        for(var i = startTime + T; i <= endTime; i += T){
            var date = new Date(i);
            if(date.getDay() == 0 || date.getDay() == 6){  //此处为节假日逻辑
                holidays++;
            }
            console.log(date);
        }
        return getWorkDate(new Date(endTime), holidays);
    } else {
        return startDate;
    }
}
getWorkDate(new Date(),10)

日志输出:

Fri Aug 06 2021 17:42:02 GMT+0800 (中国标准时间)
Sat Aug 07 2021 17:42:02 GMT+0800 (中国标准时间)
Sun Aug 08 2021 17:42:02 GMT+0800 (中国标准时间)
Mon Aug 09 2021 17:42:02 GMT+0800 (中国标准时间)
Tue Aug 10 2021 17:42:02 GMT+0800 (中国标准时间)
Wed Aug 11 2021 17:42:02 GMT+0800 (中国标准时间)
Thu Aug 12 2021 17:42:02 GMT+0800 (中国标准时间)
Fri Aug 13 2021 17:42:02 GMT+0800 (中国标准时间)
Sat Aug 14 2021 17:42:02 GMT+0800 (中国标准时间)
Sun Aug 15 2021 17:42:02 GMT+0800 (中国标准时间)
Mon Aug 16 2021 17:42:02 GMT+0800 (中国标准时间)
Tue Aug 17 2021 17:42:02 GMT+0800 (中国标准时间)
Wed Aug 18 2021 17:42:02 GMT+0800 (中国标准时间)
Thu Aug 19 2021 17:42:02 GMT+0800 (中国标准时间)

第四种方式

function getWorkDate(curentDate, nextWorkDays) {
    var T = 24 * 60 * 60 * 1000;
    var start= curentDate.getTime() + T;
    var end = start + nextWorkDays * T;
    var calDate=new Date();
    var en = function(start, end) {
        var holidays=0;
        for(var d = start; d < end; d += T){
            calDate.setTime(d);
            var day = calDate.getDay();
            if(day == 0 || day == 6){//此处为节假日逻辑
                holidays++;
            }
        }
        return holidays ? holidays + en(end, end+holidays*T) : 0;
    }
    calDate.setTime(curentDate.getTime() + (nextWorkDays + en(start, end))*T);
    return calDate;
}

第一种和第二种方式参考了腾讯社区的用户回答;原文链接:https://cloud.tencent.com/developer/ask/175878

第三种和第四种方式参考了CSDN博主「hhbmobile」的文章;原文链接:https://blog.csdn.net/TLoveTLove/article/details/8526503

标签: 获取工作日 n天