element的日期组件-两个的和单个的组件

16 篇文章 2 订阅

 

 

dateOne.vue

<template>
   <div class="dateDiv">
      <el-date-picker
        :popper-class="mrClass"
        v-model="inputVal"
        type="datetime"
        format="yyyy-MM-dd HH:mm"
        :editable = false
        :clearable = false
        prefix-icon = false
        @change="change"
        :placeholder='placeholder'>
      </el-date-picker>
    </div>
</template>

<script>
  export default {
    props:['placeholder','inputVal'],
    data() {
      return {
        inputVal: '',
        mrClass:'dateStyle',
      };
    },
     methods:{
      change(data) {
        let oDate = data.getTime();//毫秒数
        let nowDate = new Date().getTime();//当前时间毫秒数

        if(oDate > nowDate){//开始时间不能大于当前时间
          this.$message({
              message: '当前时间不能大于当前时间',
              type: 'warning'
          })
          this.inputVal = ''
          return;
        }
        let str = {
          'data' : data,
          'dataTime' : oDate,
        }
        this.$emit('validDate',str)

      },
    }

  };
</script>
<style lang='less' scoped>
  .dateDiv{
    width:100%;
    height:100%;
    .el-date-editor.el-input, .el-date-editor.el-input__inner{
      width:100%;
    }
  }

</style>

date.vue == 两个的日期选择框

<template>
  <div class="blockDate">
    <el-date-picker
      v-show = title
      ref='searchForm'
      v-model="value1"
      type="datetimerange"
      format="yyyy-MM-dd HH:mm"
      :editable = false
      :clearable = false
      prefix-icon = false
      range-separator="-"
      @change="change"
      start-placeholder="开始日期"
      end-placeholder="结束日期">
    </el-date-picker>
  </div>
</template>

<script>
  export default {
    props:['title'],
    data() {
      return {
        value1: ['', ''],
        title:true,
      };
    },
     methods:{
      change(data) {
        let oDate1 = data[0]
        let oDate2 = data[1]
        let nowDate = new Date();
        if(oDate1 && oDate2){

          if ( oDate1.getTime() > oDate2.getTime() ) {//开始时间不能大于结束时间
            this.$message({
              message: '开始时间不能大于结束时间',
              type: 'warning'
            })
            this.searchForm.applyDateMax = ''
            return;
          }

          if(oDate1.getTime() > nowDate.getTime()){//开始时间不能大于当前时间
            this.$message({
                message: '开始时间不能大于当前时间',
                type: 'warning'
            })
            return;
          }

           if(oDate2.getTime() > nowDate.getTime()){//结束时间不能大于当前时间
            this.$message({
                message: '结束时间不能大于当前时间',
                type: 'warning'
            })
            return;
          }
          let str = [];
              str[0] = oDate1;
              str[1] = oDate2;
          this.$emit('validDate',str)
        }

      },
      getDate(e){
        this.$emit('validDate',e)
      }
    }
  };
</script>
<style lang='less' scoped>
  .blockDate{
    width:100%;
    height:100%;
    .el-date-editor{
      width:100%;
    }
    .el-range-editor .el-range-input{
      border:1px solid #0090ff;
    }
  }

</style>

调动方法:以单个的为例

在template里面加入:

<date class="qxxxDate" @validDate = 'ChageStartTime' :title='dateShow' :inputVal='jcxxDate.startVal'></date>
import date from '@/components/date/dateOne'//时间选择器
components: {
      date,
    },
 ChageStartTime(data){//开始时间
        let nowTime = data.dataTime+300000;
        this.jcxxDate.startVal = this.commonjs.formatDate(data.data,'yyyy-MM-dd h:m');
        this.jcxxDate.endVal = this.commonjs.getNewDate(nowTime,'yyyy-MM-dd h:m');
      },//this.commonjs.formatDate是将时间转换为yyyy-mm-dd h:m格式,第一个参数是数据,第二个是格式

这里的this.commonjs.formatDate 公共方法请看这里:

https://blog.csdn.net/qq_41619796/article/details/94406138

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浩星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值