圖檔上這樣
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmL2gzNxADMzYTM5ATOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
首先說思路,上面的日期是循環出來顯示的,我設定的是七天
OK 放代碼 先wxml
<view class='time'> <view wx:for="{{ timeArr }}" wx:for-item="timeItem" wx:for-index="timeIndex" data-Tindex="{{ timeIndex }}" data-time="{{ timeItem.time}}" bindtap='selectTime' class='listItem {{ timeItem.time==orderTime? "current":"" }}' wx:key=''> <text>{{timeItem.time}}</text> </view> </view>
然後JS
function getThisMonthDays(year, month) {
return new Date(year, month, 0).getDate();
}
// 計算每月第一天是星期幾
function getFirstDayOfWeek(year, month) {
return new Date(Date.UTC(year, month - 1, 1)).getDay();
}
const date = new Date();
const cur_year = date.getFullYear();
const cur_month = date.getMonth() + 1;
const cur_date = date.getDate();
const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
//利用構造函數建立對象
function calendar(date, week) {
this.date = cur_year + '-' + cur_month + '-' + date;
if (date == cur_date) {
this.week = "今天";
} else if (date == cur_date + 1) {
this.week = "明天";
} else {
this.week = '星期' + week;
}
}
//目前月份的天數
var monthLength = getThisMonthDays(cur_year, cur_month)
//目前月份的第一天是星期幾
var week = getFirstDayOfWeek(cur_year, cur_month)
var x = week;
for (var i = 1; i <= monthLength; i++) {
//當循環完一周後,初始化再次循環
if (x > 6) {
x = 0;
}
//利用構造函數建立對象
that.data.calendar[i] = new calendar(i, [weeks_ch[x]][0])
x++;
}
//限制要渲染的月曆資料天數為7天以内
var flag = that.data.calendar.splice(cur_date, that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length : 7)
that.setData({
calendar: flag
})
}