element-ui 日期选择器快捷时间

pickerOptions: {
    disabledDate(time) {
        return time.getTime() > Date.now();
    },
    shortcuts: [
        {
            text: '昨日',
            onClick(picker) {
                this.relativeTime = 'now-1d'
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                console.log(this.relativeTime)

            }
        },
        {
            text: '近3日',
            onClick(picker) {
                console.log(picker)
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 3);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-3d'
            }
        },
        {
            text: '近7日',
            onClick(picker) {
                console.log(picker)
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-7d'
            }
        }, {
            text: '近30日',
            onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-30d'
            }
        },
        {
            text: '近60日',
            onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 60);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-60d'
            }
        },

        {
            text: '近90日',
            onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-90d'
            }
        },
        {
            text: '近180日',
            onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-180d'
            }
        },
        {
            text: '近365日',
            onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-365d'
            }
        },
        {
            text: '近3周',
            onClick(picker) {
                const oDate = new Date();
                oDate.setTime(oDate.getTime() - 3600 * 1000 * 24 * 21);

                var day = oDate.getDay()

                var start = new Date(),
                    end = new Date();
                if (day == 0) {
                    start.setDate(oDate.getDate());
                    end.setDate(oDate.getDate() + 21);
                } else {
                    start.setTime(oDate.getTime() - 3600 * 1000 * 24 *(day-1));
                    end.setTime(oDate.getTime() + 3600 * 1000 * 24 * (21- day));
                }

                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-3w'
            }
        },
        {
            text: '近6周',
            onClick(picker) {
                const oDate = new Date();
                oDate.setTime(oDate.getTime() - 3600 * 1000 * 24 * 42);

                var day = oDate.getDay()

                var start = new Date(),
                    end = new Date();
                if (day == 0) {
                    start.setDate(oDate.getDate());
                    end.setDate(oDate.getDate() + 21);
                } else {
                    start.setTime(oDate.getTime() - 3600 * 1000 * 24 *(day-1));
                    end.setTime(oDate.getTime() + 3600 * 1000 * 24 * (42- day));
                }

                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-6w'
            }
        },
        {
            text: '近12周',
            onClick(picker) {
                const oDate = new Date();
                oDate.setTime(oDate.getTime() - 3600 * 1000 * 24 * 84);

                var day = oDate.getDay()

                var start = new Date(),
                    end = new Date();
                if (day == 0) {
                    start.setDate(oDate.getDate());
                    end.setDate(oDate.getDate() + 21);
                } else {
                    start.setTime(oDate.getTime() - 3600 * 1000 * 24 *(day-1));
                    end.setTime(oDate.getTime() + 3600 * 1000 * 24 * (84- day));
                }

                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-12w'
            }
        },
        {
            text: '近24周',
            onClick(picker) {
                const oDate = new Date();
                oDate.setTime(oDate.getTime() - 3600 * 1000 * 24 * 168);

                var day = oDate.getDay()

                var start = new Date(),
                    end = new Date();
                if (day == 0) {
                    start.setDate(oDate.getDate());
                    end.setDate(oDate.getDate() + 21);
                } else {
                    start.setTime(oDate.getTime() - 3600 * 1000 * 24 *(day-1));
                    end.setTime(oDate.getTime() + 3600 * 1000 * 24 * (168- day));
                }

                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-24w'
            }
        },
        {
            text: '近3个月',
            onClick(picker) {
                const oDate = new Date();
                var year = oDate.getFullYear();
                var month = oDate.getMonth();
                var start, end;
                if (month == 0) {
                    year--
                    start = new Date(year, 11, 1)
                    end = new Date(year, 11, 31)
                } else {
                    start = new Date(year, month - 3, 1)
                    end = new Date(year, month, 0);
                }

                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-3m'
            }
        },
        {
            text: '近6个月',
            onClick(picker) {
                const oDate = new Date();
                var year = oDate.getFullYear();
                var month = oDate.getMonth();
                var start, end;
                if (month == 0) {
                    year--
                    start = new Date(year, 11, 1)
                    end = new Date(year, 11, 31)
                } else {
                    start = new Date(year, month - 6, 1)
                    end = new Date(year, month, 0);
                }

                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-6m'

            }
        },
        {
            text: '近12个月',
            onClick(picker) {
                const oDate = new Date();
                var year = oDate.getFullYear();
                var month = oDate.getMonth();
                var start, end;
                if (month == 0) {
                    year--
                    start = new Date(year, 11, 1)
                    end = new Date(year, 11, 31)
                } else {
                    start = new Date(year, month - 12, 1)
                    end = new Date(year, month, 0);
                }

                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-12m'

            }
        },
        {
            text: '近24个月',
            onClick(picker) {
                const oDate = new Date();
                var year = oDate.getFullYear();
                var month = oDate.getMonth();
                var start, end;
                if (month == 0) {
                    year--
                    start = new Date(year, 11, 1)
                    end = new Date(year, 11, 31)
                } else {
                    start = new Date(year, month - 24, 1)
                    end = new Date(year, month, 0);
                }

                picker.$emit('pick', [start, end]);
                this.relativeTime = 'now-24m'

            }
        },
        {
            text: '月至今',
            onClick(picker) {
                let date = new Date()
                if (date.getDate() == 1) {
                    const oDate = new Date();
                     let year = oDate.getFullYear();
                     let month = oDate.getMonth();
                     let start, end;
                    if (month == 0) {
                         year--
                        start = new Date(year, 11, 1)
                         end = new Date(year, 11, 31)
                     } else {
                         start = new Date(year, month - 1, 1)
                         end = new Date(year, month, 0);
                     }
                    picker.$emit('pick', [start, end]);
                    this.relativeTime = 'far1'
                } else {
                    const end = new Date();
                    const start = new Date();
                    start.setDate(1);
                    end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                    picker.$emit('pick', [start, end]);
                    this.relativeTime = 'far1'
                }
            }
        },
        {
            text: '年至今',
            onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setMonth(0);
                start.setDate(1);
                end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
                picker.$emit('pick', [start, end]);
                this.relativeTime = 'far12'
            }
        },


    ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值