本文實例為大家分享了微信小程序當(dāng)前時間時段選擇器的實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
DEMO效果圖

插件思路
準(zhǔn)備工作
獲取時段
準(zhǔn)備階段的JS
constructor() { this.now = new Date(); this.nowYear = this.now.getYear(); //當(dāng)前年 this.nowMonth = this.now.getMonth(); //當(dāng)前月 this.nowDay = this.now.getDate(); //當(dāng)前日 this.nowDayOfWeek = this.now.getDay(); //今天是本周的第幾天 this.nowYear += (this.nowYear < 2000) ? 1900 : 0;}//格式化數(shù)字formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n}//格式化日期formatDate(date) { let myyear = date.getFullYear(); let mymonth = date.getMonth() + 1; let myweekday = date.getDate(); return [myyear, mymonth, myweekday].map(this.formatNumber).join('-');}//獲取某月的天數(shù)getMonthDays(myMonth) { let monthStartDate = new Date(this.nowYear, myMonth, 1); let monthEndDate = new Date(this.nowYear, myMonth + 1, 1); let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24); return days;}//獲取本季度的開始月份getQuarterStartMonth() { let startMonth = 0; if (this.nowMonth < 3) { startMonth = 0; } if (2 < this.nowMonth && this.nowMonth < 6) { startMonth = 3; } if (5 < this.nowMonth && this.nowMonth < 9) { startMonth = 6; } if (this.nowMonth > 8) { startMonth = 9; } return startMonth;}時段函數(shù)JS
//獲取今天的日期 getNowDate() { return this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay)); } //獲取本周的開始日期 getWeekStartDate() { return this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 1)); } //獲取本周的結(jié)束日期 getWeekEndDate() { return this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek + 1))); } //獲取本月的開始日期 getMonthStartDate() { return this.formatDate(new Date(this.nowYear, this.nowMonth, 1)); } //獲取本月的結(jié)束日期 getMonthEndDate() { return this.formatDate(new Date(this.nowYear, this.nowMonth, this.getMonthDays(this.nowMonth))); } //獲取本季度的開始日期 getQuarterStartDate() { return this.formatDate(new Date(this.nowYear, this.getQuarterStartMonth(), 1)); } //獲取本季度的結(jié)束日期 getQuarterEndDate() { return this.formatDate(new Date(this.nowYear, this.getQuarterStartMonth() + 2, this.getMonthDays(this.getQuarterStartMonth() + 2))); } //獲取本年的開始日期 getYearStartDate() { return this.formatDate(new Date(this.nowYear, 0, 1)); } //獲取本年的結(jié)束日期 getYearEndDate() { return this.formatDate(new Date(this.nowYear, 11, 31)); }
新聞熱點
疑難解答