<!-- 时间选择器 -->
<template>
<el-date-picker
type="datetimerange"
start-placeholder="开始日期"
end-placeholder="结束日期"
value-format="yyyy-MM-dd HH:mm:ss"
picker-options="pickerOptions"
@change="handleChange"
v-bind="$attrs"
v-on="$listeners">
</el-date-picker>
</template>
<script>
import moment from 'moment';
export default {
name: 'date-picker',
data() {
return {
pickerOptions: {
shortcuts: [
{
text: '今天',
onClick(picker) {
const end = moment().endOf('day')
.toDate();
const start = moment().startOf('day')
.toDate();
picker.$emit('pick', [start, end]);
}
},
{
text: '昨天',
onClick(picker) {
const end = moment().subtract(1, 'day')
.endOf('day')
.toDate();
const start = moment().subtract(1, 'day')
.startOf('day')
.toDate();
picker.$emit('pick', [start, end]);
}
},
{
text: '最近7天',
onClick(picker) {
const end = moment().endOf('day')
.toDate();
const start = moment().subtract(6, 'day')
.startOf('day')
.toDate();
picker.$emit('pick', [start, end]);
}
},
{
text: '最近30天',
onClick(picker) {
const end = moment().endOf('day')
.toDate();
const start = moment().subtract(29, 'day')
.startOf('day')
.toDate();
picker.$emit('pick', [start, end]);
}
}
]
}
}
},
methods: {
/**
* 用户确定选定的值时触发
* @param date
*/
handleChange(date) {
this.$emit('dateChange', date);
}
}
}
</script>