请问js如何选出未来20天的工作日抽取出来?
发布于 7 年前 作者 dpc761218914 2739 次浏览 来自 问答
这样可以实现排班表了……

来自酷炫的 CNodeMD

3 回复

剔除工作日,打印出时间……

来自酷炫的 CNodeMD

    Date.DEFAULT_FORMAT_STR = "{year}年{month}月{day}日";
    Date.prototype.format = function(formatStr) {
        var month = (this.getMonth() + 1) + "",
            day = this.getDate() + "",
            date = {
                "year": this.getFullYear(),
                "month": month.length < 2 ? "0" + month : month,
                "day": day.length < 2 ? "0" + day : day
            };
        return Date.replaceTpl(date);
    }
    Date.prototype.isWorkDay = function() {
        //0 Sunday  6 Saturday
        if (this.getDay() == 0 || this.getDay() == 6) return false;
        return true;
    }
    Date.replaceTpl = function(data, formatStr) {
        formatStr = formatStr || Date.DEFAULT_FORMAT_STR;
        return formatStr.replace(/{(\w+)}/g, function(match, $1) { return data[$1]; });
    }
    Date.prototype.futureWorkDateList = function(days) {
        var day = this.getDate() + 1;
        var result = [];
        while (days) {
            var future = new Date();
            future.setDate(day);
            if (future.isWorkDay()) {
                result.push(future.format());
                days--;
            }
            day++;
        }
        return result;
    }
    console.log(new Date().futureWorkDateList(20));

@godghdai 如果我没看错,单单排除周六和周天应该是不行的,还有节假日。刚已经用moment.js 和 节假日数组 实现了~

来自酷炫的 CNodeMD

回到顶部