获取当前时间 (年月日选择框)
效果图
代码
<el-date-picker size="small" v-model="date" type="daterange" range-separator="至" unlink-panels :picker-options="pickerOptions" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
data(){
//搜索条件
date: [], // 时间年月日
pickerOptions: {
shortcuts: [{
text: '当天',
onClick(picker) {
const end = new Date();
const start = new Date();
picker.$emit('pick', [start, end]);
}
}, {
text: '七天',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
picker.$emit('pick', [start, end]);
}
}, {
text: '一个月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
picker.$emit('pick', [start, end]);
}
}, {
text: '三个月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
picker.$emit('pick', [start, end]);
}
}]
},
}
created(){
// 当前年月日
var now = new Date();
var year = now.getFullYear(); //得到当前年份
var month = now.getMonth()+1; //默认得到月份是上一个月,如果当前是3月,这个值为2月
var endOfDate = new Date(year, month, 0).getDate(); // 获取本月最后一天
month = month.toString().padStart(2, "0"); //当小于10时,显示为01.02.03
this.startdate = `${year}-${month}-01`;//拼接日期
this.enddate = `${year}-${month}-${endOfDate}`;
this.date[0]=this.startdate;
this.date[1]=this.enddate;
}