一个简单的时间筛选的插件

效果展示:

201812417128

class Process {
    /***
     *
     * @param start 开始时间 eg:'2018-06-01 10:00:00'
     * @param end   结束时间 eg: '2019-03-25 10:00:00'
     * @param delay 推迟时间 eg:3
     * @param weeks 周期购   eg: [ 1, 5, 6] 表示只筛选周一,周五,周六
     */
    constructor(start, end, delay=0, weeks=[0,1,2,3,4,5,6]) {
        this.week = weeks;
        this.startTime = start;
        this.endTime = end;
        this.purchase_times = delay;
        this.start = '';
        this.end = '';
        this.purchase_times = this.computedWeek();
    }
    //获取时间范围
    getAllTimes(){
        let start = this.startTime.split(' ')[0];
        let end = this.endTime.split(' ')[0];
        return this.getAll(start,end)
    }

    // 计算推迟后的星期
    computedWeek(){
        let week = this.week;
        let weeks =  week.map(item=>{
            return (item + this.purchase_times)%7
        })
        return weeks
    }
    // 计算时间范围
    timeRange() {
        let start = this.startTime.split(' ')[0];
        let end = this.endTime.split(' ')[0] + 1;
        this.start = this.dateToNs(start);
        this.end = this.dateToNs(end);
    }

    // 将时间转化为时间戳
    dateToNs(date) {
        let thisTime = date;
        thisTime = thisTime.replace(/-/g, '/');
        let time = new Date(thisTime);
        return time.getTime();
    }

    // 获取年份
    getYear(ns) {
        return new Date(ns).getYear();
    }

    // 返回当前日期
    format(ns) {
        let s = '';
        let date = new Date(parseInt(ns));
        let mouth = (date.getMonth() + 1) >= 10 
                ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1));
        let day = date.getDate() >= 10 ? date.getDate() : ('0' + date.getDate());
        let year = date.getFullYear();
        s += year + '-'; // 获取年份。
        s += mouth + "-"; // 获取月份。
        s += day; // 获取日。
        return {
            mouth,
            yearMouth: year + '年 ' + mouth + '月',
            s,
            day
        }; // 返回日期。
    }
    // 返回日期为星期几
    getDay(date) {
        return new Date(date).getDay();
    }

    //获取所有的日期
    getAll(begin, end) {
        let obj = {};
        let ab = begin.split("-");
        let ae = end.split("-");
        let db = new Date();
        db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
        let de = new Date();
        de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
        let unixDb = db.getTime() - 24 * 60 * 60 * 1000;
        let unixDe = de.getTime() - 24 * 60 * 60 * 1000;
        for (let k = unixDb; k <= unixDe;) {
            k = k + 24 * 60 * 60 * 1000;
            let {
                mouth,
                yearMouth,
                s,
                day
            } = this.format(k)

            if (obj[yearMouth]) {
                obj[yearMouth].push({
                    time: s,
                    week: this.getDay(s),
                    mouth,
                    day
                })
            } else {
                obj[yearMouth] = [{
                    time: s,
                    week: this.getDay(s),
                    mouth,
                    day
                }]
            }
        }

        return this.filterDate(obj)
    }

    //按照周期筛选
    filterDate(obj){
        let week = this.week;
        let arr = []
        Object.keys(obj).map(item=>{
            let arr2 =  obj[item].filter(i=>{
                let index = week.findIndex(key=>key==i.week)
                if(index!=-1) return item
            })
            arr.push({year: item,days: arr2})
        })
        return arr
    }
}

export default Process = Process;

然后,简单的使用一下:

initData() {
    let start = '2018-06-01 10:00:00';
    let end = '2019-03-25 10:00:00';
    let delay = 3;
    let weeks = [ 1, 5, 6];
    this.timeData = new Process(start, end, delay, weeks).getAllTimes();
}
Last Updated: 12/4/2018, 5:09:56 PM