微信小程序----當前時間的時段選擇器插件(今天、本週、本月、本季度、本年、自定義時段)

DEMO效果圖

當前時間的時段選擇器插件效果圖


插件思路


準備工作

  1. 獲取當前時間,同時獲取當前的年、月、日、周幾;
  2. 創建處理日期數字的函數;
  3. 創建格式化日期的函數;
  4. 創建獲取某月天數的函數;
  5. 創建獲取季度開始的月份函數。

獲取時段

  1. 創建獲取當天的時段函數;
  2. 創建獲取本週的時段函數;
  3. 創建獲取本月的時段函數;
  4. 創建獲取本季度的時段函數;
  5. 創建獲取本年的時段函數;
  6. 創建自定義時段函數。

準備階段的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");
  1. 使用getperiod.js
this.time = new GetPeriod();

//獲取本年的結束日期
let end = this.time.getYearEndDate();

項目地址

微信小程序—-時段選取插件

git clone git@github.com:Rattenking/GetPeriod.git

DEMO下載

我的博客,歡迎交流!

我的CSDN博客,歡迎交流!

微信小程序專欄

前端筆記專欄

微信小程序實現部分高德地圖功能的DEMO下載

微信小程序實現MUI的部分效果的DEMO下載

微信小程序實現MUI的GIT項目地址

微信小程序實例列表

前端筆記列表

遊戲列表

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章