element ui 中时间选择器周 展示为2020-11-2 -2020-11-8 形式
-
因为element周时间选择器选中时展示的出来的样式是yyyy年mm周这样样式而我们有时候需要的是2020-11-2 - 2020-11-8这种形式。写了几种改变value值的方法都是失败了,然后想到了在创建一个input输入框覆盖到时间选择器上,让时间选择器改为透明也可以实现类似效果。以下是代码
-
效果就是这样
-
html部分
<!-- :picker-options="{'firstDayOfWeek': 1}" 设置起始时间为周一-->
<div class="block" v-if="input1=='周'" style="position: relative;top: 0;">
<el-date-picker :editable="false" style=" opacity: 0;position: absolute;
z-index: 100;" size='small' :picker-options="{'firstDayOfWeek': 1}" type="week" v-model="seletWeekValue" format="yyyy年第WW周" :clearable="false"
:default-time="['00:00:00', '23:59:59']" @change="changeweek" placeholder="选择查询时间">
</el-date-picker>
<div>
<el-input size='small' placeholder="请选择日期" v-model="weekTime">
<i slot="suffix" class="el-input__icon el-icon-date"></i>
</el-input>
</div>
</div>
- js部分
// An highlighted block
let page = new Vue({
el: "#app",
data: {
seletWeekValue: "",
weekTime: "",
startWeekTimeNum: "",
endWeekTimeNum: "",
}, methods: {
// 时间戳转换为时间
timestampToTime(timestamp) {
var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
let Y = date.getFullYear() + '-';
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
let D = date.getDate() + ' ';
return Y + M + D;
},
//周选择器开始时间和结束时间
changeweek(val) {
//val===> Mon Nov 09 2020 00:00:00 GMT+0800 (中国标准时间)
let todayDay= val?new Date(val):new Date();
let todayOfWeek = todayDay.getDay();
let todayOfmonth = todayDay.getMonth();
let todayOfDate = todayDay.getDate();
let spendDay = 1;
if (todayOfWeek != 0) {
spendDay = 7 - todayOfWeek;//开始时间到结束时间还有几天
}
// 86400000为一天的毫秒数
this.startTimeNum = todayDay.valueOf() - (6 - spendDay) * 86400000;//开始时间时间戳
this.endTimeNum = todayDay.valueOf() + spendDay * 86400000;//结束时间时间戳
let startTimeStr = this.timestampToTime(this.startWeekTimeNum); // 时间戳转字符串
let endTimeStr = this.timestampToTime(this.endWeekTimeNum);
this.weekTime = startTimeStr + " - " + endTimeStr; // 选择的范围
console.log(this.weekTime);
},
}
})