Dater.js
1 /** 2 * 换算某月的上个月,下月,本月的月信息,日历写死展示42天, 3 * 可以通过更改: 4 * var nextlen = 42 - (prem.length + getMonthDay[m - 1]); 42改成35 5 * 上月和下月不显示数据可以可以注释掉相应月的赋值代码,修改此对象: 6 * this.monthDays = { 7 preMonthDays: prem, //上月的天 8 thisMonthDays: im, //本月的天 9 nextMonthDays: nextm, //下月的天 10 day: d, 11 month: m, 12 year: y 13 } 14 */ 15 export default function Dater (y, m, d) { 16 var date = new Date() 17 18 // 获取年,月,天 19 var y = y || date.getFullYear(), 20 m = m || date.getMonth() + 1, 21 d = d || date.getDate() 22 23 // 是否为闰年,闰年能被4和400整除,不能被100整除 24 var isLeapYear = function (years) { 25 return !!((years % 4 == 0 && years % 100 !== 0) || (years % 400 === 0)) 26 } 27 28 // 获取月的一号是星期几, 0是星期日 29 var getFirstDay = function (y, m, d) { 30 var d = new Date(y, m, d) 31 d.setDate(1) 32 return d.getDay() 33 } 34 35 // 每月有多少天,一年12月的天数 36 var getMonthDay = [31, isLeapYear(y) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 37 38 // prem 上月的天信息,im本月的天信息, nextm下个月的天信息, lastmd上个月的天数 39 var prem = [], im = [], nextm = [], lastmd = getMonthDay[m - 1 - 1] 40 41 //设置上月的天数信息 42 // 该月的1号是不是周日, 43 if (getFirstDay(y, m - 1, d) === 0) { 44 for (var i = 6; i >= 0; i--) { 45 prem[6 - i] = lastmd - i 46 } 47 } else { 48 for (var i = getFirstDay(y, m - 1, d); i > 0; i--) { 49 prem[getFirstDay(y, m - 1, d) - i] = lastmd - i + 1 50 } 51 } 52 53 // 设置本月的天数信息 54 for (var i = 0; i < getMonthDay[m - 1]; i++) { 55 im[i] = i + 1 56 } 57 58 // 设置下月的天信息 59 // 日历展示42天 60 var nextlen = 42 - (prem.length + getMonthDay[m - 1]) 61 for (var i = 0; i < nextlen; i++) { 62 nextm[i] = i + 1 63 } 64 65 66 if (isNaN(prem[0])) { 67 for (let i = 0; i < prem.length; i++) { 68 prem[i] = 31 - prem.length + i + 1 69 } 70 } 71 72 73 this.monthDays = { 74 preMonthDays: prem, //上月的天 75 thisMonthDays: im, //本月的天 76 nextMonthDays: nextm, //下月的天 77 day: d, 78 month: m, 79 year: y 80 } 81 82 // 没有看明白此算法的作用,在日历中没有使用this.weekDays数据 83 var arr = [] 84 for (var i = 0; i < 7; i++) { 85 var weekDate = new Date() 86 weekDate.setFullYear(y) 87 weekDate.setMonth(m - 1) 88 weekDate.setDate(d - 1) 89 var weekday = weekDate.getDay() // 获取星期几0-6 90 var newday = weekDate.getDate() + 1 - weekday + i // 91 weekDate.setDate(newday - 1) 92 arr.push({ 93 year: weekDate.getFullYear(), 94 month: weekDate.getMonth() + 1, 95 day: weekDate.getDate() + 1 96 }) 97 } 98 this.weekDays = { 99 data: arr,100 day: d,101 month: m,102 year: y103 }104 // var array = []105 // for (var i = 0; i < prem.length; i++) { 106 // array.push(`${y}-${m - 1}-${prem[i]}`)107 // }108 // for (var i = 0; i < im.length; i++) { 109 // array.push(`${y}-${m}-${im[i]}`)110 // }111 // for (var i = 0; i < nextm.length; i++) { 112 // array.push(`${y}-${m + 1}-${nextm[i]}`)113 // }114 // console.log(array)115 // var arrayIndex = 0;116 // for (var i = 0; i < array.length; i++) { 117 // if(m === Number(array[i].split('-')[1]) && d === Number(array[i].split('-')[2])) { 118 // arrayIndex = i119 // break120 // }121 // }122 };123 124 Dater.change = function (anchor, type, time) {125 var date = new Date()126 date.setFullYear(anchor.year, anchor.month - 1, anchor.day)127 switch (type) {128 case 'y': {129 date.setFullYear(date.getFullYear() + time)130 break131 }132 case 'm': {133 date.setMonth(date.getMonth() + time)134 break135 }136 case 'w': {137 date.setDate(date.getDate() + time * 7)138 break139 }140 case 'd': {141 date.setDate(date.getDate() + time)142 break143 }144 }145 return {146 year: date.getFullYear(),147 month: date.getMonth() + 1,148 day: date.getDate()149 }150 }151 152 Dater.getTodayAnchor = function () {153 var date = new Date()154 return {155 year: date.getFullYear(),156 month: date.getMonth() + 1,157 day: date.getDate()158 }159 }160 Dater.AnchorToDayString = function (anchor) {161 // return '' + anchor.year + (anchor.month < 10 ? '0' : '') + anchor.month + (anchor.day < 10 ? '0' : '') + anchor.day162 // console.log( `${anchor.year}/${anchor.month < 10 ? '0': ''}${anchor.month}/${anchor.day < 10 ? '0' : ''}${anchor.day}`)163 return `${anchor.year}/${anchor.month < 10 ? '0': ''}${anchor.month}/${anchor.day < 10 ? '0' : ''}${anchor.day}`164 }
Calendar.wx
1