微信小程序当前时间时段选择器插件使用方法详
网络编程 2021-07-04 15:50www.168986.cn编程入门
这篇文章主要为大家详细介绍了微信小程序当前时间时段选择器插件使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了微信小程序当前时间时段选择器的实现代码,供大家参考,具体内容如下
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)); }
使用方法
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.:Rattenking/GetPeriod.git
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 甘肃哪有关键词排名优化购买方式有哪些
- 甘肃SEO如何做网站优化
- 河南seo关键词优化怎么做电话营销
- 北京SEO优化如何做QQ群营销
- 来宾百度关键词排名:提升您网站曝光率的关键
- 卢龙关键词优化:提升您网站排名的策略与技巧
- 山东网站优化的注意事项有哪些
- 四川整站优化怎样提升在搜索引擎中的排名
- 疏附整站优化:提升网站性能与用户体验的全新
- 海南seo主要做什么工作售后服务要做到哪些
- 荣昌百度网站优化:提升您网站的搜索引擎排名
- 河北seo网站排名关键词优化如何做SEO
- 江西优化关键词排名推广售后保障一般有哪些
- 古浪SEO优化:提升你的网站可见性
- 西藏网站排名优化怎么把网站排名在百度首页
- 如何提升阳东百度快照排名:详尽指南