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
}
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));
}
使用方法
- 引入getperiod.js
const GetPeriod = require("../../utils/getperiod.js");
- 使用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项目地址
微信小程序实例列表
前端笔记列表
游戏列表