获取到的时间就像上图中一样,特此为自己做个记录:
getTime: function() {
this.tabBarList = []
let weekarr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
for (var i = 0; i <= 6; i++) {
const time = new Date(new Date().setDate((new Date().getDate() + i)));
const month = `0${time.getMonth()}`.slice(-2); //月
const strDate = `0${time.getDate()}`.slice(-2); //号
const weeks = `${time.getDay()}`.slice(-2); //星期
if (i == 0) {
this.tabBarList.push({
date: `${month}-${strDate}`,
id: i,
name: "今天"
})
// 把当前的月份日期存储到本地
this.date = `${month}-${strDate}`;
} else if (i == 1) {
this.tabBarList.push({
date: `${month}-${strDate}`,
id: i ,
name: "明天"
})
} else {
this.tabBarList.push({
date: `${month}-${strDate}`,
id: i,
name: weekarr[weeks]
})
}
}
},
其中:1. new Date() , setDate() 和 getDate()
new Date() 获取的是当前的日期
setDate() 设置天数,并且必须接收一个整数。,如:
表示一个月中的一天的一个数值(1 ~ 31)
getDate() 返回的是本月份的几天
2.关于slice要注意:slice是截取的作用,通常里面有两个值,分别表示开始和结束的索引,但是当参数变成负值的时候,要先加上原本的长度才开始截取,比如这里的slice(-2)实际上就是slice(1).
更加详细的解释可以看看这个文章,作者在里面详细的阐述了关于slice以及substring,substr方法传入负值的时候的变化:https://blog.csdn.net/weixin_38382659/article/details/90267317