直接调用getTimes()即可:
转化的字段可以自行设置weekDay数组;
日期时间样式也可以自行转换设置。
getWeekqq(dateString) {
var weekDay = ["周天", "周一", "周二", "周三", "周四", "周五", "周六"];
var myDate = new Date(Date.parse(dateString));
return weekDay[myDate.getDay()]
},
getTimes() {
var date = new Date();
var base = Date.parse(date); // 转换为时间戳
var year = date.getFullYear(); //获取当前年份
var mon = date.getMonth() + 1; //获取当前月份
var day = date.getDate(); //获取当前日
var oneDay = 24 * 3600 * 1000
var daytime = `${year}-${mon >= 10 ? mon : '0' + mon}-${day >= 10 ? day : '0' + day}`; //今日时间
var daytimeArr = [{
data: daytime,
week: this.getWeekqq(daytime),
day: `${mon}/${day}`
}]
for (var i = 1; i < 7; i++) { //今天以及后6天
var now = new Date(base += oneDay);
var myear = now.getFullYear();
var month = now.getMonth() + 1;
var mday = now.getDate()
daytimeArr.push({
data: [myear, month >= 10 ? month : '0' + month, mday >= 10 ? mday : '0' + mday].join('-'),
week: this.getWeekqq([myear, month >= 10 ? month : '0' + month, mday >= 10 ? mday : '0' + mday].join('-')),
day: [month, mday].join('/')
})
}
console.log(daytimeArr)
return daytimeArr
},
打印console.log(daytimeArr)控制台输出结果: