选择日期默认月初到月末

<el-form-item label="选择日期">
<el-date-picker
v-model="dateValue"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
unlink-panels
style="240px"
value-format="yyyy-MM-dd"
:clearable="false"
@change="changeDate"
/>
</el-form-item>


<script>
import { startToEnd } from '@/utils'
export default {
name: 'IncomeSearch',
props: {
type: {
type: String,
default: 'income'
}
},
data() {
return {
searchForm: {
startdate: startToEnd()[0],
enddate:startToEnd()[1],
},
dateValue: startToEnd(),
}
},
methods: {
changeDate(val) {
this.searchForm.startdate = val[0]
this.searchForm.enddate = val[1]
}
}
}
</script>

utils/index.js

// 取当月第一天到最后一天
export function startToEnd(step = 0) {
const now = new Date()
const nowYear = now.getFullYear()
const nowMonth = now.getMonth()
const monthStart = new Date(nowYear, nowMonth + step, 1)
const monthEnd = new Date(nowYear, nowMonth + 1 + step, 0)
const arr = []
arr.push(parseTime(+new Date(monthStart), '{y}-{m}-{d}'))
arr.push(parseTime(+new Date(monthEnd), '{y}-{m}-{d}'))
return arr
}

//如果是上个月第一天到最后一天

startToEnd(-1)

原文地址:https://www.cnblogs.com/hellofangfang/p/13530153.html