本文實(shí)例為大家分享了微信小程序當(dāng)前時(shí)間時(shí)段選擇器的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
DEMO效果圖
插件思路
準(zhǔn)備工作
獲取時(shí)段
準(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;}//獲取本季度的開(kāi)始月份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í)段函數(shù)JS
//獲取今天的日期 getNowDate() { return this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay)); } //獲取本周的開(kāi)始日期 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))); } //獲取本月的開(kāi)始日期 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))); } //獲取本季度的開(kāi)始日期 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))); } //獲取本年的開(kāi)始日期 getYearStartDate() { return this.formatDate(new Date(this.nowYear, 0, 1)); } //獲取本年的結(jié)束日期 getYearEndDate() { return this.formatDate(new Date(this.nowYear, 11, 31)); }
使用方法
1.引入getperiod.js
const GetPeriod = require("../../utils/getperiod.js");
2.使用getperiod.js
this.time = new GetPeriod();//獲取本年的結(jié)束日期let end = this.time.getYearEndDate();
項(xiàng)目地址
git clone [email protected]:Rattenking/GetPeriod.git
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選