(编辑:jimmy 日期: 2025/10/28 浏览:2)
本文实例为大家分享了微信小程序当前时间时段选择器的实现代码,供大家参考,具体内容如下
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) "htmlcode">
//获取今天的日期
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。