微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段)
2021/5/17 12:55:08
本文主要是介绍微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
创建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
这篇关于微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-22微信小程序的接口信息py可以抓到吗?-icode9专业技术文章分享
- 2024-11-22怎样解析出微信小程序二维码带的参数?-icode9专业技术文章分享
- 2024-11-22微信小程序二维码怎样解析成链接?-icode9专业技术文章分享
- 2024-11-22微信小程序接口地址的域名需要怎么设置?-icode9专业技术文章分享
- 2024-11-22微信小程序的业务域名有什么作用-icode9专业技术文章分享
- 2024-11-22微信小程序 image有类似html5的onload吗?-icode9专业技术文章分享
- 2024-11-22微信小程序中怎么实现文本内容超出行数后显示省略号?-icode9专业技术文章分享
- 2024-11-22微信小程序怎么实现分享样式定制和图片定制功能?-icode9专业技术文章分享
- 2024-11-20微信小程序全栈教程:从零开始的全攻略
- 2024-11-19微信小程序全栈学习:从零开始的完整指南