js 筛选时间(本年、本月、本季等)

js 筛选时间(本年、本月、本季等)

//获取近len天
                $getday(len) {
                    let days = [];
                    var date = new Date();
                    for(let i=0; i<=24*(len-1);i+=24){        //今天加上前len-1天
                        let ob = date.getTime() - i * 60 * 60 * 1000
                        let dateItem=new Date(ob);    //使用当天时间戳减去以前的时间毫秒(小时*分*秒*毫秒)
                        let y = dateItem.getFullYear();    //获取年份
                        let m = dateItem.getMonth() + 1;    //获取月份js月份从0开始,需要+1
                        let d= dateItem.getDate();    //获取日期
                        m = this.$lessTen(m);    //给为单数的月份补零
                        d = this.$lessTen(d);    //给为单数的日期补零
                        let valueItem= y + '-' + m + '-' + d;    //组合
                        days.push(valueItem);    //添加至数组
                    }
                    let str = days[days.length-1] + ' 00:00:00:::' + days[0] + ' 23:59:59';
                
                    return str;        
                },
                // 小于10 前面加 0
                $lessTen(num) {
                    let str = 0;
                    if (Number(num) < 10) {
                        str = '0' + num;
                    } else {
                        str = '' + num;
                    }
                    return str;
                },
                
                // 筛选时间
                $initDateTime(type) {
                    // type  1: 本周    2 本月        3 本季        4 本年    5  近7天    6 近30天    7 近90天
                    let now = new Date();
                    let str = '';
                    let y = now.getFullYear();
                    let m = now.getMonth() + 1;
                    let d = now.getDate();
                    switch (type) {
                        case 1:
                            let weekFirstDay = new Date(now - (now.getDay() - 1) * 86400000);
                            let firstMonth = Number(weekFirstDay.getMonth()) + 1;
                            str = weekFirstDay.getFullYear() + '-' + this.$lessTen(firstMonth) + '-' + this
                                .$lessTen(weekFirstDay.getDate()) + ' 00:00:00:::' + y + '-' + this.$lessTen(
                                m) + '-' + this.$lessTen(d) + ' 23:59:59';
                            break;
                        case 2:
                            str = y + '-' + this.$lessTen(m) + '-01 00:00:00:::' + y + '-' + this.$lessTen(m) +
                                '-' + this.$lessTen(d) + ' 23:59:59';
                            break;
                        case 3:
                            if (m < 4) {
                                str = y + '-01-01 00:00:00:::' + y + '-' + this.$lessTen(m) + '-' + this
                                    .$lessTen(d) + ' 23:59:59';
                            } else if (m < 7) {
                                str = y + '-04-01 00:00:00:::' + y + '-' + this.$lessTen(m) + '-' + this
                                    .$lessTen(d) + ' 23:59:59';
                            } else if (m < 10) {
                                str = y + '-07-01 00:00:00:::' + y + '-' + this.$lessTen(m) + '-' + this
                                    .$lessTen(d) + ' 23:59:59';
                            } else {
                                str = y + '-10-01 00:00:00:::' + y + '-' + this.$lessTen(m) + '-' + this
                                    .$lessTen(d) + ' 23:59:59';
                            }
                            break;
                        case 4:
                            str = y + '-01-01 00:00:00:::' + y + '-' + this.$lessTen(m) + '-' + this.$lessTen(
                                d) + ' 23:59:59';
                            break;
                        case 5:
                            str = this.$getday(7);
                            break;
                        case 6:
                            str = this.$getday(30);
                            break;
                        case 7:
                            str = this.$getday(90);
                            break;
                        default:
                            str = '';
                    };
                    return str;
                }

 

posted on 2022-05-18 16:06  梦幻飞雪  阅读(618)  评论(0编辑  收藏  举报