微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段)
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-10-04微信小程序如何实现点击图片查看大图功能-icode9专业技术文章分享
- 2024-09-28uniapp 小程序获取到数据库的换行符怎么展示-icode9专业技术文章分享
- 2024-09-20微信小程序全栈项目实战:从零开始的全面指南
- 2024-09-20微信小程序项目实战:零基础入门教程
- 2024-09-19微信小程序项目中miniprogram_npm文件跟node_modules文件的差别-icode9专业技术文章分享
- 2024-08-27小程序 单行显示,超出部分省略号显示的方法代码-icode9专业技术文章分享
- 2024-08-27微信小程序如何获取到特种设备的刘海屏高度-icode9专业技术文章分享
- 2024-08-27微信小程序如何获取苹果手机底部一横线高度信息方法代码-icode9专业技术文章分享
- 2024-08-27微信小程序 顶部状态栏标签背景为none但还是看不到状态栏什么原因-icode9专业技术文章分享
- 2024-08-21微信小程序全栈入门:从零开始的一站式指南