微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段)
DEMO效果图
插件思路
准备工作
- 获取当前时间,同时获取当前的年、月、日、周几;
- 创建处理日期数字的函数;
- 创建格式化日期的函数;
- 创建获取某月天数的函数;
- 创建获取季度开始的月份函数。
获取时段
- 创建获取当天的时段函数;
- 创建获取本周的时段函数;
- 创建获取本月的时段函数;
- 创建获取本季度的时段函数;
- 创建获取本年的时段函数;
- 创建自定义时段函数。
准备阶段的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
}
//格式化日期
form