ElementUI 時間選擇器,顯示默認時間為當前時間至7天之前時間


一、element框架時間選擇器,默認顯示起止時間為:當前日期往前推7天

1、html結構:

<el-form :inline="true" :model="deviceFormData" ref="deviceFormData" class="hardware-form">
    <el-form-item label="查詢時間:">
         <el-date-picker
                :editable="false"
                :clearable="false"
                v-model="deviceFormData.time"
                type="daterange"
                range-separator="至"
                start-placeholder="開始日期"
                end-placeholder="結束日期"
                :picker-options="pickerOptions"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd">
         </el-date-picker>
    </el-form-item>
</el-form>

2、js部分:

data(){
  return {
    deviceFormData:{
       time:[]
    },
    //不能選擇當前日期之后的時間
    pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
  }  
}

created() {
    if (process.client) {
      const that = this;
      that.getTimeFn();
    }
  },   

/**
     * 設置默認時間
     */
    getTimeFn() {
      const that = this;
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      that.deviceFormData.time[0] = that.formatDate(start);
      that.deviceFormData.time[1] = that.formatDate(end);
    },
    /**
     * 格式化時間
     */
    formatDate(date) {
      var myyear = date.getFullYear();
      var mymonth = date.getMonth() + 1;
      var myweekday = date.getDate();

      if (mymonth < 10) {
        mymonth = "0" + mymonth;
      }
      if (myweekday < 10) {
        myweekday = "0" + myweekday;
      }
      return myyear + "-" + mymonth + "-" + myweekday;
    },

3、效果預覽:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM