日期的时候是从周日开始的,后查明因为各国星期开始的第一天是不同的,所以才会这样,在控件中增加 :picker-options="{'firstDayOfWeek': 1}" 属性来进行控制,可选范围 1-7
<el-form-item label="周期:" prop="misCycle">
<el-date-picker
v-model="temp1.misCycle"
:picker-options="{'firstDayOfWeek': 1}"
type="week"
format="yyyy 第 WW 周"
placeholder="请选择周期"
style="width:100%"
@change="setWeekNum"
/>
</el-form-item>
data() {
return {
temp1: {
// 周期
misCycle: '',
},
// 周期开始时间
startDate: '',
// 周期结束时间
endDate: '',
// 周期时间
finalDate: ''
}
},
methods: {
p(s) {
return s < 10 ? '0' + s : s
},
// 时间
setWeekNum() {
console.log(this.temp1.misCycle)
const newTime = this.temp1.misCycle.getTime()
const day = this.temp1.misCycle.getDay()
const oneDayTime = 24 * 60 * 60 * 1000
const fromTime = newTime - day * oneDayTime
const endTime = newTime + (7 - day) * oneDayTime
const fromDate = new Date(fromTime)
this.startDate = fromDate.getFullYear() + '\xa0' + this.p((fromDate.getMonth() + 1)) + '\xa0' + this.p(fromDate.getDate() + 1)
const toDate = new Date(endTime)
this.endDate = toDate.getFullYear() + '\xa0' + this.p((toDate.getMonth() + 1)) + '\xa0' + this.p(toDate.getDate())
console.log(this.startDate)
console.log(this.endDate)
this.finalDate = this.startDate + '-' + this.endDate
console.log(this.finalDate)
}
}