這篇文章主要介紹微信小程序中日歷組件的實(shí)現(xiàn)示例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
最近在做微信小程序項(xiàng)目,其中涉及到日歷。一直以來(lái),遇到日歷,就是網(wǎng)上隨便找個(gè)插件,這次心血來(lái)潮,想著自己去實(shí)現(xiàn)一下。這次不是封裝功能強(qiáng)大,健碩完美的組件,只是記錄一下,主體思路。更多功能還得根據(jù)項(xiàng)目需要,自己去挖掘、實(shí)現(xiàn)。(大佬輕噴)
// 獲取某年某月總共多少天 getDateLen(year, month) { let actualMonth = month - 1; let timeDistance = +new Date(year, month) - +new Date(year, actualMonth); return timeDistance / (1000 * 60 * 60 * 24); },
當(dāng)Date作為構(gòu)造函數(shù)調(diào)用并傳入多個(gè)參數(shù)時(shí),如果數(shù)值大于合理范圍時(shí)(如月份為13或者分鐘數(shù)為70),相鄰的數(shù)值會(huì)被調(diào)整。比如 new Date(2013, 13, 1)等于new Date(2014, 1, 1),它們都表示日期2014-02-01(注意月份是從0開(kāi)始的)。其他數(shù)值也是類(lèi)似,new Date(2013, 2, 1, 0, 70)等于new Date(2013, 2, 1, 1, 10),都表示時(shí)間2013-03-01T01:10:00。
// 獲取某月1號(hào)是周幾 getFirstDateWeek(year, month) { return new Date(year, month - 1, 1).getDay() },
// 獲取當(dāng)月數(shù)據(jù),返回?cái)?shù)組 getCurrentArr(){ let currentMonthDateLen = this.getDateLen(this.data.currentYear, this.data.currentMonth) // 獲取當(dāng)月天數(shù) let currentMonthDateArr = [] // 定義空數(shù)組 if (currentMonthDateLen > 0) { for (let i = 1; i <= currentMonthDateLen; i++) { currentMonthDateArr.push({ month: 'current', // 只是為了增加標(biāo)識(shí),區(qū)分上下月 date: i }) } } this.setData({ currentMonthDateLen }) return currentMonthDateArr },
// 上月 年、月 preMonth(year, month) { if (month == 1) { return { year: --year, month: 12 } } else { return { year: year, month: --month } } },
// 獲取當(dāng)月中,上月多余數(shù)據(jù),返回?cái)?shù)組 getPreArr(){ let preMonthDateLen = this.getFirstDateWeek(this.data.currentYear, this.data.currentMonth) // 當(dāng)月1號(hào)是周幾 == 上月殘余天數(shù)) let preMonthDateArr = [] // 定義空數(shù)組 if (preMonthDateLen > 0) { let { year, month } = this.preMonth(this.data.currentYear, this.data.currentMonth) // 獲取上月 年、月 let date = this.getDateLen(year, month) // 獲取上月天數(shù) for (let i = 0; i < preMonthDateLen; i++) { preMonthDateArr.unshift({ // 尾部追加 month: 'pre', // 只是為了增加標(biāo)識(shí),區(qū)分當(dāng)、下月 date: date }) date-- } } this.setData({ preMonthDateLen }) return preMonthDateArr },
// 下月 年、月 nextMonth(year, month) { if (month == 12) { return { year: ++year, month: 1 } } else { return { year: year, month: ++month } } },
// 獲取當(dāng)月中,下月多余數(shù)據(jù),返回?cái)?shù)組 getNextArr() { let nextMonthDateLen = 42 - this.data.preMonthDateLen - this.data.currentMonthDateLen // 下月多余天數(shù) let nextMonthDateArr = [] // 定義空數(shù)組 if (nextMonthDateLen > 0) { for (let i = 1; i <= nextMonthDateLen; i++) { nextMonthDateArr.push({ month: 'next',// 只是為了增加標(biāo)識(shí),區(qū)分當(dāng)、上月 date: i }) } } return nextMonthDateArr },
[ {month: "pre", date: 30}, {month: "pre", date: 31}, {month: "current", date: 1}, {month: "current", date: 2}, ... {month: "current", date: 31}, {month: "next", date: 1}, {month: "next", date: 2} ]
以上是“微信小程序中日歷組件的實(shí)現(xiàn)示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱(chēng):微信小程序中日歷組件的實(shí)現(xiàn)示例-創(chuàng)新互聯(lián)
分享地址:http://chinadenli.net/article28/dgjejp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、定制開(kāi)發(fā)、品牌網(wǎng)站制作、網(wǎng)站改版、軟件開(kāi)發(fā)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容