最近公司要作一个酒店入住的小程序,不可避免的必定会使用到日历,而小程序没有内置的日历组件。在网上看了一下也没有很是适合需求的日历,因而本身写了一个。小程序
写一个日历只须要知道两件事情:数组
因为是酒店入住的日历,因此须要实现以下功能:数据结构
根据最后的功能实现,我生成的每月的数据结构以下:this
{ year: 2018, month: 3, fullMonth: '03', allDays:[ { day: 1, fullDay: '01', fullDate: '2018-03-01' }, { day: 2, fullDay: '02', fullDate: '2018-03-02' }, //...... //(后面的数据同上) ] }
year就是年份,month是月份,day是日期,fullDate是完整日期。
fullMonth和fullDay本来是两个不须要的数据,可是在点击日期改变颜色的时候用到了,由于小程序没有提供很好的处理数据的filter。固然这个问题也和个人我的水平有关,若是有哪位大神有更好的方法,请留言告诉我。我很是想去掉这两个数据。code
// calendar.js文件 Page({ data: { week_list: ['日','一','二','三','四','五','六'], startDate: '', endDate: '', date_click: 0 }, // 获取每个月总天数 getAllDaysOfMonth(year,month) { return new Date(year,month,0).getDate(); }, // 获取每个月第一天是星期几 getFirstDayOfMonth(year,month) { return new Date(year, month - 1, 1).getDay(); }, // 计算本月前空了几格 getEmptyGrids(year,month) { // FirstDayOfMonth表明本月的第一天是星期几 const FirstDayOfMonth = this.getFirstDayOfMonth(year, month); let emptyGrids = []; // 有空格的状况 if (FirstDayOfMonth > 0) { for (let i = 0; i < FirstDayOfMonth; i++) { emptyGrids.push({ 'num': '', 'fullDate': 'x' //x是我本身定义的一个值,表明没有日期 }); } // 将空格放入数组 return emptyGrids; }else{ // 不然返回一个新数组 return []; } }, // 计算本月日历 getDaysOfThisMonth(year,month) { let days = []; const AllDaysOfMonth = this.getAllDaysOfMonth(year, month); let fullMonth = month.toString().length === 1 ? `0${month}`:month; for (let i = 0; i < AllDaysOfMonth; i++) { let day = i+1, fullDay = day; fullDay = fullDay.toString().length === 1 ? `0${day}` : fullDay; days.push({ day, fullDay, 'fullDate': `${year}-${fullMonth}-${fullDay}` }); } // 返回每月的具体日期 return days; }, // 循环渲染日历 // 从本月开始渲染,n表明包括本月开始连续渲染几个月 fillCalendar(n) { let year = this.data.cur_year, month = this.data.cur_month, fullMonth, canlendar_data = []; // 计算年月以及具体日历 for (let i = this.data.cur_month; i < this.data.cur_month + n; i++) { let EmptyGrids = this.getEmptyGrids(year, month); let DaysOfThisMonth = this.getDaysOfThisMonth(year, month); // 把空格和具体日历合为一个数组 let allDays = [...EmptyGrids, ...DaysOfThisMonth]; // 对年份和月份的计算作一些判断 if (month > 12) { year++; month = 1; fullMonth = '01' canlendar_data.push({ year, month, fullMonth, allDays }); month++; }else{ fullMonth = month.toString().length === 1 ? `0${month}` : month; canlendar_data.push({ year, month, fullMonth, allDays }); month++; } } this.setData({ canlendar_data }) }, onLoad() { const date = new Date(); const cur_year = date.getFullYear(); const cur_month = date.getMonth() + 1; const cur_day = date.getDate(); this.setData({ date, cur_year, cur_month, cur_day }) let month = this.data.cur_month.toString().length === 1 ? `0${this.data.cur_month}` : this.data.cur_month; let day = this.data.cur_day.toString().length === 1 ? `0${this.data.cur_day}` : this.data.cur_day; let nowDate = `${cur_year}-${month}-${day}`; this.setData({ nowDate }) this.fillCalendar(6); }, // 点击日期 chooseDate(e) { const year_click = e.currentTarget.dataset.year; const month_click = e.currentTarget.dataset.month; const day_click = e.currentTarget.dataset.day; console.log(year_click,month_click,day_click); // 若是是空格或者之前的日期就直接返回 if(day_click === ''||`${year_click}-${month_click}-${day_click}` < this.data.nowDate) { return; } // 获取点击对象的id let id = e.currentTarget.dataset.id; // data_click为0表明选择的是入住日期,不然就是离店日期 if (this.data.date_click == 0){ // 选择入住日期 this.setData({ startDate: `${year_click}-${month_click}-${day_click}`, date_click: 1 }) }else { let newDay = new Date(Date.parse(id)); let oldDay = new Date(Date.parse(this.data.startDate)); // 判断第二次点击的日期在第一次点击的日期前面仍是后面 if (newDay > oldDay) { this.setData({ endDate: `${year_click}-${month_click}-${day_click}`, date_click: 2 }) }else{ this.setData({ startDate: `${year_click}-${month_click}-${day_click}`, endDate: '', date_click: 1 }) } } } })
<!-- calendar.wxml文件 --> <view class="container"> <view id="week"> <view class="week-item {{idx===0||idx===6?'relax':''}}" wx:for="{{week_list}}" wx:for-index="idx">{{item}}</view> </view> <scroll-view scoll-y="true"> <view class="month-block" wx:for="{{canlendar_data}}" wx:for-item="canlendar_item"> <view class="month-title">{{canlendar_item.year}}年{{canlendar_item.month}}月</view> <view class="month-content"> <view class="month-day {{item.fullDate<nowDate?'gray':''}} {{startDate===item.fullDate?'startActive':''}} {{endDate===item.fullDate?'endActive':''}} {{item.fullDate>startDate&&item.fullDate<endDate&&startDate!==''&&endDate!==''?'midActive':''}}" bindtap="chooseDate" data-year="{{canlendar_item.year}}" data-month="{{canlendar_item.fullMonth}}" data-day="{{item.fullDay}}" data-id="{{item.fullDate}}" wx:for="{{canlendar_item.allDays}}">{{item.day}}</view> </view> </view> </scroll-view> </view>
{{idx===0||idx===6?'relax':''}} 是改变周六周日的颜色,
{{item.fullDate<nowDate?'gray':''}} 是改变过去日期的颜色,
{{startDate===item.fullDate?'startActive':''}} 判断点击的是入住日期,
{{endDate===item.fullDate?'endActive':''}} 判断点击的是离店日期,
{{item.fullDate>startDate&&item.fullDate<endDate&&startDate!==''&&endDate!==''?'midActive':''}} 改变入住日期和离店日期之间的日期颜色xml
到此一个简单的日历就完成了,固然这个日历没法知足全部业务需求,可是基本的日历渲染功能以及点击选择功能都有。因此在业务需求之上对其进行小部分改变就能够了,但愿你们能够留言指出个人问题,我也会进一步的改善这个日历代码。对象