编程语言
首页 > 编程语言> > 微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段)

微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段)

作者:互联网

创建getperiod.js

class GetPeriod{
  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;
  }
  //获取今天的日期
  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));
  }
  //获取时段方法
  getPeriod(obj){
    let opts = obj || {},time = null;
    opts = {
      periodType: opts.periodType || 'now',
      spaceType: opts.spaceType || '~'
    }
    function formatNumber(param1, param2){
      return [param1, param2].join(opts.spaceType);
    }
    if (opts.periodType == 'week'){
      time = formatNumber(this.getWeekStartDate(), this.getWeekEndDate());
    } else if (opts.periodType == 'month'){
      time = formatNumber(this.getMonthStartDate(), this.getMonthEndDate());
    } else if (opts.periodType == 'quarter') {
      time = formatNumber(this.getQuarterStartDate(), this.getQuarterEndDate());
    } else if (opts.periodType == 'year') {
      time = formatNumber(this.getYearStartDate(), this.getYearEndDate());
    } else {
      time = formatNumber(this.getNowDate(), this.getNowDate());
    }
    return time;
  }
}
module.exports = GetPeriod;

小程序中引用:

const GetPeriod = require("../../utils/getperiod.js");

小程序中使用:

// 在 onl oad 周期函数中 new GetPeriod(),并且用变量接收
Page({
  onl oad(){
    this.time = new GetPeriod();
  }
})

各方法的应用

//获取今天的日期
let nowDate = this.time.getNowDate();
console.log(nowDate)
//2018-06-05

//获取本周的开始日期
let startWeek = this.time.getWeekStartDate();
console.log(startWeek)
//2018-06-04

//获取本周的结束日期
let endWeek = this.time.getWeekEndDate();
console.log(endWeek)
//2018-06-10

//获取本月的开始日期
let startMonth = this.time.getMonthStartDate();
console.log(startMonth)
//2018-06-01

//获取本月的结束日期
let endMonth = this.time.getMonthEndDate();
console.log(endMonth)
//2018-06-30

//获取本季的开始日期
let startQuarter = this.time.getQuarterStartDate();
console.log(startQuarter)
//2018-04-01

//获取本季的结束日期
let endQuarter = this.time.getQuarterEndDate();
console.log(endQuarter)
//2018-06-30

//获取本年的开始日期
let startYear = this.time.getYearStartDate();
console.log(startYear)
//2018-01-01

//获取本年的结束日期
let endYear = this.time.getYearEndDate();
console.log(endYear)
//2018-12-31

 

标签:return,自定义,let,时段,time,new,nowMonth,nowYear,选择器
来源: https://www.cnblogs.com/zhpblog/p/14776426.html