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

插件思路
準備工作
- 獲取當前時間,同時獲取當前的年、月、日、周幾;
- 創(chuàng)建處理日期數字的函數;
- 創(chuàng)建格式化日期的函數;
- 創(chuàng)建獲取某月天數的函數;
- 創(chuàng)建獲取季度開始的月份函數。
獲取時段
- 創(chuàng)建獲取當天的時段函數;
- 創(chuàng)建獲取本周的時段函數;
- 創(chuàng)建獲取本月的時段函數;
- 創(chuàng)建獲取本季度的時段函數;
- 創(chuàng)建獲取本年的時段函數;
- 創(chuàng)建自定義時段函數。
準備階段的JS
constructor() {
this.now = new Date();
this.nowYear = this.now.getYear(); //當前年
this.nowMonth = this.now.getMonth(); //當前月
this.nowDay = this.now.getDate(); //當前日
this.nowDayOfWeek = this.now.getDay(); //今天是本周的第幾天
this.nowYear += (this.nowYear < 2000) ? 1900 : 0;
}
//格式化數字
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('-');
}
//獲取某月的天數
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;
}
時段函數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));
}
//獲取本周的結束日期
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));
}
//獲取本月的結束日期
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));
}
//獲取本季度的結束日期
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));
}
//獲取本年的結束日期
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(); //獲取本年的結束日期 let end = this.time.getYearEndDate();
項目地址
git clone git@github.com:Rattenking/GetPeriod.git
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ie中js創(chuàng)建checkbox默認選中問題探討
js創(chuàng)建checkbox默認選中在某些特殊情況下還是比較實用的,下面有個不錯的示例,大家可以參考下2013-10-10
JAVA Web實時消息后臺服務器推送技術---GoEasy
本篇文章主要介紹了PHP實現(xiàn)Web實時消息后臺服務器推送技術,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2016-11-11

