最近有需求,列表查询条件的日期组件自动展示前一周到今天的日期。做个小记。废话不多说,直接上代码。
html:
<el-date-picker style="width:23%" v-model="time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" ></el-date-picker>
js:计算属性
computed: { // 默认时间 timeDefault () { let date = new Date() // 通过时间戳计算 let defalutStartTime = date.getTime() - 7 * 24 * 3600 * 1000 // 转化为时间戳 let defalutEndTime = date.getTime() let startDateNs = new Date(defalutStartTime) let endDateNs = new Date(defalutEndTime) // 月,日 不够10补0 defalutStartTime = startDateNs.getFullYear() + '-' + ((startDateNs.getMonth() + 1) >= 10 ? (startDateNs.getMonth() + 1) : '0' + (startDateNs.getMonth() + 1)) + '-' + (startDateNs.getDate() >= 10 ? startDateNs.getDate() : '0' + startDateNs.getDate()) defalutEndTime = endDateNs.getFullYear() + '-' + ((endDateNs.getMonth() + 1) >= 10 ? (endDateNs.getMonth() + 1) : '0' + (endDateNs.getMonth() + 1)) + '-' + (endDateNs.getDate() >= 10 ? endDateNs.getDate() : '0' + endDateNs.getDate()) return [defalutStartTime, defalutEndTime] } },
created生命周期里直接赋值
created(){ this.time = this.timeDefault; },
以上即可。