VUE-Element组件(10)el-date-picker

本文详细介绍了Element UI中El-DatePicker组件的使用方法,包括如何设置日期格式、初始化日期、限制日期选择范围及进行日期有效性验证等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、绑定值为字符串类型:

<el-date-picker
            placeholder="考试开始时间"
            align="center"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            class="long"
            v-model="startTime"
            placement="bottom-start"
            type="date"
            :editable="false"
            style="width:100%"
            size="mini"
            @change="refreshMap"
          ></el-date-picker>

初始化:

this.startTime = "2021-12-21"

2、绑定值类型为Date:

<el-date-picker
                v-model="searchData.startDate"
                type="date"
                placeholder="开始日期"
                style="width:100%"
                size="mini"
              >
              </el-date-picker>

初始化为昨天:

created() {
    this.searchData.startDate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000).format('yyyy-MM-dd');
    this.searchData.endDate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000).format('yyyy-MM-dd');
    this.loadData();
  },

时间校验

 isDateValid() {
      if (!this.searchData.startDate || !this.searchData.endDate) {
        this.$Message.error('时间为必填');
        return false;
      }
      this.searchData.startDate = new Date(Date.parse(this.searchData.startDate));
      this.searchData.endDate = new Date(Date.parse(this.searchData.endDate));
      if (this.searchData.startDate.getTime() > this.searchData.endDate.getTime()) {
        this.$Message.error('开始时间不可以大于结束时间');
        return false;
      }
      let time = this.searchData.endDate.getTime() - this.searchData.startDate.getTime();
      let day = time / (1000 * 60 * 60 * 24);
      console.info('时间差' + day);
      if (day > 7) {
        this.$Message.error('时间范围不可以超过7天');
        return false;
      }
      this.searchData.startDate.setHours(0, 0, 0, 0);
      this.searchData.endDate.setHours(0, 0, 0, 0);
      return true;
    },

注意最后.setHours(0, 0, 0, 0)是因为发现初始化的日期默认为早上8点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值