1、项目场景
默认el-date-picker组件时间为上月月初到月末显示时间区间范围格式
2、代码
<template>
<el-card>
<div class="wrap-box">
<div class="block">
<span class="demonstration">时间</span>
<el-date-picker
v-model="value"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
value-format="yyyyMMdd"
></el-date-picker>
<el-button class="query" type="primary">查询</el-button>
</div>
</div>
</el-card>
</template>
<script>
export default {
data () {
return {
value: [],
}
},
created () {
//查询上一个月份的月初到月末
this.getLastMonthDate()
},
methods: {
getLastMonthDate () {
let myDate = new Date()
let month = myDate.getMonth()
month = month < 10 ? "0" + month : month //格式化月份,补0
let statDateStart = myDate.getFullYear().toString() + month + "01" //月初
let dayEnd = new Date(myDate.getFullYear(), month, 0).getDate() //获取当月一共有多少天
let statDateEnd = myDate.getFullYear().toString() + month + dayEnd //月末
// console.log(statDateStart)
// console.log(statDateEnd)
this.value.push(statDateStart, statDateEnd)
}
},
};
</script>