因為項目需要,做了一個類似電商的時間預(yù)約功能,覺得有用,就獨立出來成了個小插件。
部分js代碼
-
var that=this;
-
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 = ['日', '一', '二', '三', '四', '五', '六'];
-
//利用構(gòu)造函數(shù)創(chuàng)建對象
-
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;
-
}
-
}
-
//當(dāng)前月份的天數(shù)
-
var monthLength= getThisMonthDays(cur_year, cur_month)
-
//當(dāng)前月份的第一天是星期幾
-
var week = getFirstDayOfWeek(cur_year, cur_month)
-
var x = week;
-
for(var i=1;i<=monthLength;i++){
-
//當(dāng)循環(huán)完一周后,初始化再次循環(huán)
-
if(x>6){
-
x=0;
-
}
-
//利用構(gòu)造函數(shù)創(chuàng)建對象
-
that.data.calendar[i] = new calendar(i, [weeks_ch[x]][0])
-
x++;
-
}
-
//限制要渲染的日歷數(shù)據(jù)天數(shù)為7天以內(nèi)(用戶體驗)
-
var flag = that.data.calendar.splice(cur_date, that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length:7)
-
that.setData({
-
calendar: flag
-
})
-
//設(shè)置scroll-view的子容器的寬度
-
that.setData({
-
width: 186 * parseInt(that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length : 7)
-
})
源代碼地址https://github.com/Dorr2333/calendar-and-order.git
|