日期时间选择器 el-date-picker 设置开始时间和结束时间

先对组件进行渲染
在这里插入图片描述

<el-date-picker
      v-model="callTime"
      type="datetimerange"
      :picker-options="pickerOptions"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      align="right"
      @change="getTimeList"
    >
    </el-date-picker>
pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },

!!!动态绑定的数据一定要是数组形式的callTime: [],callTime: ""会报错
数据设置:开始时间 结束时间

var data = JSON.stringify({
        startDate: this.callTime[0],
        endDate: this.callTime[1],
      });

设置默认时间为上个月

created() {
    // 设置默认时间为上个月
    var now = new Date();
    var year = now.getFullYear(); //得到当前年份
    var month = now.getMonth(); //默认得到月份是上一个月,如果当前是3月,这个值为2月
    if (month == 12)
      //如果当前是1月,则获取到的数据为12月,年份减一
      year = year - 1;
    var nextMonth = month + 1; //其实就是当前月份
    month = month.toString().padStart(2, "0"); //当小于10时,显示为01.02.03
    nextMonth = nextMonth.toString().padStart(2, "0");
    this.startDate = `${year}-${month}-01`; //拼接日期
    this.endDate = `${year}-${nextMonth}-01`;
    // console.log(this.startDate + "  " + this.endDate);
    this.callTime[0] = this.startDate;
    this.callTime[1] = this.endDate;
    this.getTimeList(); //调用方法查询指定时间段内的数据
  },
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值