微信小程序當(dāng)前時(shí)間時(shí)段選擇器插件使用方法詳解
本文實(shí)例為大家分享了微信小程序當(dāng)前時(shí)間時(shí)段選擇器的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
DEMO效果圖
插件思路
準(zhǔn)備工作
- 獲取當(dāng)前時(shí)間,同時(shí)獲取當(dāng)前的年、月、日、周幾;
- 創(chuàng)建處理日期數(shù)字的函數(shù);
- 創(chuàng)建格式化日期的函數(shù);
- 創(chuàng)建獲取某月天數(shù)的函數(shù);
- 創(chuàng)建獲取季度開始的月份函數(shù)。
獲取時(shí)段
- 創(chuàng)建獲取當(dāng)天的時(shí)段函數(shù);
- 創(chuàng)建獲取本周的時(shí)段函數(shù);
- 創(chuàng)建獲取本月的時(shí)段函數(shù);
- 創(chuàng)建獲取本季度的時(shí)段函數(shù);
- 創(chuàng)建獲取本年的時(shí)段函數(shù);
- 創(chuàng)建自定義時(shí)段函數(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; } //獲取本季度的開始月份 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)); } //獲取本周的開始日期 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)); }
使用方法
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 git@github.com:Rattenking/GetPeriod.git
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
uni-app動態(tài)修改導(dǎo)航欄標(biāo)題簡單步驟
uniapp作為一款開源軟件,可以做到一端多用,不過也有局限,在開發(fā)中有時(shí)候需要動態(tài)的去修改標(biāo)題,下面這篇文章主要給大家介紹了關(guān)于uni-app動態(tài)修改導(dǎo)航欄標(biāo)題的相關(guān)資料,需要的朋友可以參考下2023-06-06javascript 處理HTML元素必須避免使用的一種方法
我們在編寫前臺頁面的時(shí)候,可能經(jīng)常會用到“javascript+數(shù)據(jù)”生成頁面元素的方法,但當(dāng)我們要處理的數(shù)據(jù)量較大,導(dǎo)致頁面需要展現(xiàn)過多的控件的時(shí)候,頁面的響應(yīng)速度也會直線下降2009-07-07JS開發(fā)中基本數(shù)據(jù)類型具體有哪幾種
JS的數(shù)據(jù)類型包括基本數(shù)據(jù)類型、復(fù)雜數(shù)據(jù)類型和特殊數(shù)據(jù)類型,今天我們主要先講解一下基本數(shù)據(jù)類型。感興趣的朋友一起看看吧2017-10-10JavaScript中的noscript元素屬性位置及作用介紹
Javascript插入到XHTML中要使用script元素,使用這個(gè)元素可以把Javascript嵌入到XHTML頁面中,讓腳本與標(biāo)記混合在一起,感興趣的朋友可以了解下2013-04-04javascript動態(tài)獲取登錄時(shí)間和在線時(shí)長
這篇文章主要為大家詳細(xì)介紹了javascript動態(tài)獲取登錄時(shí)間和在線時(shí)長的相關(guān)資料,獲得登錄時(shí)候的時(shí)間,用來和動態(tài)的時(shí)間做差來求時(shí)長,感興趣的小伙伴們可以參考一下2016-02-02