vue封装日期组件(基于elementUI)

5 篇文章 0 订阅
4 篇文章 0 订阅
  • 样式(主要自定义了左边快捷选项,可以自己自定义,默认近七日)
    在这里插入图片描述

  • props传参:[startTime,endTime] (yyyy-mm-dd)类型

  • 第三方库momentjs

<template>
    <div class="date-picker">
        <el-date-picker v-model="dateTime" type="daterange" align="right" unlink-panels range-separator="" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd" @change="chooseDate" :clearable="false"></el-date-picker>
    </div>
</template>

<script>
import moment from "moment";
export default {
    name: "date-picker",
    props: {
        date: {
            type: Array,
            required: true,
        },
    },
    data() {
        return {
            dateTime: [],
            pickerOptions: {
                //日期选择快捷选择
                shortcuts: [
                    {
                        text: "今日",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "昨日",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(moment(start).subtract(1, "days"));
                            end.setTime(moment(end).subtract(1, "days"));
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "近7日",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(moment(start).subtract(6, "days"));
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "近30日",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(moment(start).subtract(29, "days"));
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "本周",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(moment(start).weekday(0));
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "上周",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(moment(start).weekday(-7));
                            end.setTime(moment(end).weekday(-1));
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "本月",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                moment(start).subtract(
                                    moment().date() - 1,
                                    "days"
                                )
                            );
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "上月",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                moment(
                                    moment().month(moment().month() - 1)
                                ).subtract(moment().date() - 1, "days")
                            );
                            end.setTime(
                                moment(end).subtract(moment().date(), "days")
                            );
                            picker.$emit("pick", [start, end]);
                        },
                    },
                ],
            },
        };
    },
    mounted() {
        this.dateTime = this.date;
    },
    watch: {
        date(val) {
            this.dateTime = val;
        },
    },
    methods: {
        /**
         * @description 选择日期
         */
        chooseDate(val) {
            this.$emit("chooseDate", val);
        },
    },
};
</script>

<style lang='less' scoped>
.date-picker {
    .el-input__inner {
        padding: 3px 10px;
        /deep/.el-range-separator {
            padding: 0px;
        }
    }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值