element date-picker 实现每个月1号到当前日期默认时间设置(默认设置当前时间)

<el-row >
        <el-col :span="6" style="margin-left:200px">
          
          Start Time:<el-date-picker
            v-model="requestinfo.starttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            >
          el-date-picker>
      
        el-col>
        <el-col :span="6" >
          
          End Time:<el-date-picker
            v-model="requestinfo.endtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          el-date-picker>
          
        el-col>
      el-row >
export default {
  data() {
    return {

requestinfo:{
       
        starttime:'',
        endtime:'',
      },

}
}

mounted() {
    this.getCurrentTime()
    this.getCurrentMonthStart()
  },


methods{
getCurrentTime () {
      var date = new Date()
      date.setDate(1)
      var month = parseInt(date.getMonth() + 1)
      var day = date.getDate()
      if (month < 10)  month = '0' + month
      if (day < 10)  day = '0' + day
      this.requestinfo.starttime = date.getFullYear() + '-' + month + '-' + day
    },
    getCurrentMonthStart () {
      var date = new Date()
      var month = parseInt(date.getMonth() + 1)
      var day = date.getDate()

      if (month < 10)  month = '0' + month
      if (day < 10)  day = '0' + day
      this.requestinfo.endtime = date.getFullYear() + '-' + month + '-' + day
    },
    }

效果展示:
在这里插入图片描述

如果仅是获取当前时间可以用这个简便方法

<el-row >
        <el-col :span="6" style="margin-left:200px">
          
          Start Time:<el-date-picker
            v-model="requestinfo.starttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            >
          </el-date-picker>
      
        </el-col>
        <el-col :span="6" >
          
          End Time:<el-date-picker
            v-model="requestinfo.endtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
          
        </el-col>
      </el-row >


export default {
  data() {
    return {

requestinfo:{    
        starttime:new Date(),
        endtime:new Date(),    
      },

}
}

效果展示:
在这里插入图片描述

获取当前时间,以及当前的前三个月时间点

      startTime(){
        // var current_time = this.endTime()
        var date=new Date();
        date.setMonth(date.getMonth()-3)
        let ttime = date.toLocaleDateString();
        var split_time = ttime.split('/')
        this.starttime = split_time.join("-")
      },
      endTime(){
        var date=new Date();   
        var year=date.getFullYear(); //获取当前年份   
        var month=date.getMonth()+1; //获取当前月份   
        var dat=date.getDate(); //获取当前日     
        this.endtime=year+'-'+month+'-'+dat
        return this.endtime

      }

在这里插入图片描述

你可能感兴趣的:(element-ui,Vue,vue,elementui)