js 小程序获取本周七天

data: {
weekdays:['','','','','','','']
},
 
 
onLoad: function (options) {
let that = this;
let now = new Date();
let today = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
let nowDayOfWeek = now.getDay();
// console.log(today)
this.selectFn(nowDayOfWeek, today)
},
selectFn: function (n,toady) {
let weekdays = this.data.weekdays;
if(n!=0){
weekdays[n - 1] = toady;
}else{
weekdays[weekdays.length - 1] = toady
}
console.log(weekdays)
// console.log(this.getWeek(0))
for (let i = 0; i < weekdays.length;i++){
if (weekdays[i]==''){
// console.log(i)
if(i>n-1){
weekdays[i] = this.getWeek(i - n + 1)
}else{
weekdays[i] = this.getWeek(i - n + 1)
}
}
if (i >= weekdays.length-1){
console.log(weekdays)
}
}
},

getWeek: function (i) {
var now = new Date();
var firstDay = new Date(now - (now.getDay() -2) * 86400000);
firstDay.setDate(firstDay.getDate() + i);
let mon = Number(firstDay.getMonth()) + 1;
return now.getFullYear() + "-" + mon + "-" + firstDay.getDate();
},
 
["2018-3-26", "2018-3-27", "2018-3-28", "2018-3-29", "2018-3-30", "2018-3-31", "2018-4-1"]
posted @ 2018-03-27 16:59  ThisCall  阅读(590)  评论(0编辑  收藏  举报