日期选择器默认一年时间

<el-form-item label="日期:">
  <el-date-picker
    style="width: 250px"
    v-model="formNum.deliveryCharge"
    type="daterange"
    range-separator=""
    start-placeholder="开始日期"
    end-placeholder="结束日期">
  </el-date-picker>
</el-form-item>
data() {
 return {
   pageSizeNum: 10, // 每页的数据
     pageNumNum: 1, // 初始页
   formNum: {
     deliveryCharge: [], // 时间
     // 一般日期选择器都是有开始、结束时间
     transportStartTime: null,
     transportEndTime: null,
  }
}
// 在mounted里面调用
  mounted() {
      this.formNum.transportEndTime = this.getPassYearFormatDate();
      this.formNum.transportStartTime = this.getPassYearFormatDate();
  },
methods: {
    // 默认一年时间
    getPassYearFormatDate() {
        // 获取过去时间,并传值给现在时间
        var nowDate = new Date();
        var date = new Date(nowDate);
        date.setDate(date.getDate() - 365);
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
        }
        var deliveryCharge = year + seperator1 + month + seperator1 + strDate;
        this.getNowFormatDate(deliveryCharge);
    },
    getNowFormatDate(deliveryCharge) {
        // 获取现在时间,并接受过去时间的值
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
        }
        var nowData = year + seperator1 + month + seperator1 + strDate;
        this.formNum.deliveryCharge = [deliveryCharge, nowData];
      // 因为deliveryCharge是一个数组 这样拆分出来 可以开始、结束时间单独赋值   
this.yunTime1 = this.formNum.deliveryCharge[0];   this.yunTime2 = this.formNum.deliveryCharge[1]; }, }
// 如果给后台传时间的参数
let params = {
    pageNumNum: this.pageNumNum,
    pageSizeNum: this.pageSizeNum,
    transportStartTime: this.yunTime1,
    transportEndTime: this.yunTime2,
};
接口封装的函数名(params).then(res=>{}

法二:

created() {
    let lastYear = Date.parse(moment().add(-1, 'y').format("YYYY-MM-DD") + ' 00:00:00') // 去年
    let nowYear = Date.parse(moment().format('YYYY-MM-DD')+ ' 23:59:59') // 今年
    this.listQuery.choseDate = [lastYear,nowYear] // listQuery.choseDate是el-date-picker时间组件绑定的值
  // usageStartTime、usageEndTime 起止时间参数
    this.listQuery.usageStartTime = this.listQuery.choseDate[0];
    this.listQuery.usageEndTime = this.listQuery.choseDate[1];
  },

亲测有效!

 

posted @ 2021-08-03 18:48  挽你手  阅读(364)  评论(0编辑  收藏  举报