微信小程序當(dāng)前時間時段選擇器插件使用方法詳解
更新時間:2018年12月28日 10:46:33 作者:Rattenking
這篇文章主要為大家詳細介紹了微信小程序當(dāng)前時間時段選擇器插件使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序當(dāng)前時間時段選擇器的實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
DEMO效果圖

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

