elementui的时间选择器按周选择时展示具体日期范围
2021-12-13 本文已影响0人
记录学习生活
HTML代码:
<el-date-picker
v-model="week"
@change="changeTime"
:picker-options="{firstDayOfWeek: 1}"
type="week"
:format="startTimeStamp + ' 至 ' + endTimeStamp"
placeholder="选择周">
</el-date-picker>
使用 :picker-options="{firstDayOfWeek: 1}"
改变周起始日,默认是7
js代码:
changeTime(val) {
if(val) {
let timeStamp = val.getTime(); //标准时间转为时间戳,毫秒级别
this.startTimeStamp = this.timeFun(timeStamp - 24 * 60 * 60 * 1000); //开始时间
this.endTimeStamp = this.timeFun(timeStamp + (24 * 60 * 60 * 1000) * 5); //结束时间
console.log(this.startTimeStamp, this.endTimeStamp)
this.searchData.firstDate = new Date(this.startTimeStamp + ' 00:00:00').getTime() //+ ' 00:00:00' //new Date(this.startTimeStamp + ' 00:00:00').getTime() / 1000
this.searchData.lastDay = new Date(this.endTimeStamp + ' 23:59:59').getTime() //+ ' 23:59:59' //new Date(this.endTimeStamp + ' 23:59:59').getTime() / 1000
}else{
this.searchData.lastDay = undefined
this.searchData.lastDay = undefined
}
},
//时间戳转为yy-mm-dd hh:mm:ss
timeFun(unixtimestamp) {
var unixtimestamp = new Date(unixtimestamp);
var year = 1900 + unixtimestamp.getYear();
var month = "0" + (unixtimestamp.getMonth() + 1);
var date = "0" + unixtimestamp.getDate();
// var hour = "0" + unixtimestamp.getHours();
// var minute = "0" + unixtimestamp.getMinutes();
// var second = "0" + unixtimestamp.getSeconds();
return year + "-" + month.substring(month.length - 2, month.length) + "-" + date.substring(date.length - 2, date
.length)
// +
//" " + hour.substring(hour.length - 2, hour.length) + ":" +
//minute.substring(minute.length - 2, minute.length) + ":" +
//second.substring(second.length - 2, second.length);
},