需求:有一個圖表echart , 點擊查詢,得到當(dāng)前日期的 上一周 ,需要把日期展示到echart圖標(biāo)上的X軸上。
通過DatePicker 日期選擇器 需要設(shè)置 value-format指定綁定值的格式。
value-format="timestamp" 格式設(shè)置成時間戳
<el-date-picker v-model="value2" type="date" :placeholder="date" :picker-options="pickerOptions"
value-format="timestamp" @change="chooseDate">
chooseDate() {
console.log(this.value2) // 獲取當(dāng)前選擇日期的時間戳
this.getDay(this.value2, '-')
},
// 計算前7天的日期
getDay(timestamp, str) {
var today = new Date();
if (!timestamp) return;
var nowTime = timestamp;
// var ms7 = 24 * 3600 * 1000 * 7; //七天前的時間
// 設(shè)置上了七天前的時間
if (this.dateArr.length > 0) {
this.dateArr = [];
}
for (let i = 0; i < 7; i++) {
let ms7 = 24 * 3600 * 1000 * i
today.setTime(parseInt(nowTime - ms7));
var oYear = today.getFullYear();
var oMoth = (today.getMonth() + 1).toString();
if (oMoth.length <= 1) oMoth = '0' + oMoth;
var oDay = today.getDate().toString();
if (oDay.length <= 1) oDay = '0' + oDay;
// console.log(oYear + str + oMoth + str + oDay)
let timer = oMoth + str + oDay;
this.dateArr.unshift(timer)
}
console.log(this.dateArr)
},