欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 微信小程序:最近三天,当日昨日,当月,上月其他时间可以参考思路

微信小程序:最近三天,当日昨日,当月,上月其他时间可以参考思路

2024/11/30 12:37:05 来源:https://blog.csdn.net/yao_guai/article/details/141575769  浏览:    关键词:微信小程序:最近三天,当日昨日,当月,上月其他时间可以参考思路

 在下小程序中转换时间比较麻烦,这个是最近做的一个项目,涉及时间快捷方式转换开始时间及结束时间,希望对大家有帮助

getDate: function (dateType) {let _this = this;const now = new Date();if (dateType === '0') {// 默认最近三天const currentDate = new Date();currentDate.setDate(currentDate.getDate() - 3);_this.setData({date: `${_this.formatDate(currentDate)} ~ ${_this.formatDate(now)}`,startTime: _this.formatDate(currentDate) + " 00:00:00",endTime: _this.formatDate(now) + " 23:59:59",showText: '最近三天'});} else if (dateType === '1') {const currentDate = new Date();currentDate.setDate(currentDate.getDate());_this.setData({date: `${_this.formatDate(currentDate)} ~ ${_this.formatDate(now)}`,startTime: _this.formatDate(currentDate) + " 00:00:00",endTime: _this.formatDate(now) + " 23:59:59",showText: '当日'});} else if (dateType === '2') {const currentDate = new Date();currentDate.setDate(currentDate.getDate() - 1);_this.setData({date: `${_this.formatDate(currentDate)} ~ ${_this.formatDate(now)}`,startTime: _this.formatDate(currentDate) + " 00:00:00",endTime: _this.formatDate(now) + " 23:59:59",showText: '昨日'});} else if (dateType === '3') {const currentDate = new Date();const monthDayNum = _this.getMonthDays(now);const currentDays = now.getDate();currentDate.setDate(currentDate.getDate() + monthDayNum - currentDays);now.setMonth(now.getMonth(), 1);_this.setData({date: `${_this.formatDate(now)} ~ ${_this.formatDate(currentDate)}`,startTime: _this.formatDate(now) + " 00:00:00",endTime: _this.formatDate(currentDate) + " 23:59:59",showText: '当月'});}else if (dateType === '4') {now.setMonth(now.getMonth()-1, 1);const currentDate = new Date();currentDate.setMonth(currentDate.getMonth()-1, 1);const monthDayNum = _this.getMonthDays(now);currentDate.setDate(currentDate.getDate()+monthDayNum -1);_this.setData({date: `${_this.formatDate(now)} ~ ${_this.formatDate(currentDate)}`,startTime: _this.formatDate(now) + " 00:00:00",endTime: _this.formatDate(currentDate) + " 23:59:59",showText: '上月'});}},

相差多少天:

  getMonthDays: function (date) {let monthStartDate = new Date(date.getFullYear(), date.getMonth(), 1);let monthEndDate = new Date(date.getFullYear(), date.getMonth() + 1, 1);let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);return days;},

 格式化:

 formatDate(date) {date = new Date(date);const month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();return `${date.getFullYear()}-${month}-${day}`;},

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com