el-date-picker默认时间为上月(默认为当月)

项目场景:

项目场景:默认el-date-picker组件时间为上月或者本月

在这里插入图片描述

解决方案:

1.html代码

<el-date-picker
     v-model="value"
     type="month"
     placeholder="选择月份"
     value-format="yyyyMM"
>
</el-date-picker>

2.js代码

data() {
    
    
    return {
    
         
      value: "",
    };
  },
  //懒得封装方法,直接写生命周期里了,习惯代码规范的可以自行封装放入方法
created() {
    
    
    //默认显示上月日期
    let date = new Date(new Date() - 30 * 24 * 3600 * 1000);
    this.value =
      date.getFullYear() +
      (date.getMonth() + 1 < 10 ? "0" : "") +
      (date.getMonth() + 1);
    console.log(this.value );   //202212

	//默认显示当月日期
    let date = new Date(new Date());
    this.value =
      date.getFullYear() +
      (date.getMonth() + 1 < 10 ? "0" : "") +
      (date.getMonth() + 1);
    console.log(this.value );   //202301

  },

猜你喜欢

转载自blog.csdn.net/wangjiecsdn/article/details/128535378
今日推荐