当前位置: 首页 > news >正文

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

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

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}`;},

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 全桥整流器简介
  • Oracle手动误删物理上的数据文件解决办法
  • 【Linux】:用户缓冲区
  • 更新RK3588开发板的rknn_server和librknnrt.so【这篇文章是RKNPU2从入门到实践 --- 【5】的配套文章】
  • js发送邮件:如何在Node.js实现邮件发送?
  • vue 批量导出pdf 压缩包 zip
  • Location-Aware Self-Supervised Transformers for Semantic Segmentation
  • 最新保姆级教程
  • 车窗边缘上的装饰边和花点的作用
  • 废品回收小程序,从传统到现代化的回收模式
  • spring cloud与boot与cloud alibaba版本对应
  • AR 眼镜之-系统通知定制(通知弹窗)-实现方案
  • 我在IBM的时光碎片1 - 回忆昊海大厦
  • Unity (编辑器)数据持久化 之 ScriptableObject初识与创建
  • Adobe Illustrator vs Photoshop:设计界的“相声搭档”
  • 【译】JS基础算法脚本:字符串结尾
  • Android单元测试 - 几个重要问题
  • iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
  • js作用域和this的理解
  • nginx 负载服务器优化
  • PHP变量
  • React Transition Group -- Transition 组件
  • Webpack 4x 之路 ( 四 )
  • 如何解决微信端直接跳WAP端
  • 数据可视化之 Sankey 桑基图的实现
  • 小程序测试方案初探
  • 协程
  • 一、python与pycharm的安装
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • #pragma预处理命令
  • (a /b)*c的值
  • (floyd+补集) poj 3275
  • (javascript)再说document.body.scrollTop的使用问题
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (二)Eureka服务搭建,服务注册,服务发现
  • (论文阅读40-45)图像描述1
  • (一)SpringBoot3---尚硅谷总结
  • (一)基于IDEA的JAVA基础10
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (转) RFS+AutoItLibrary测试web对话框
  • (转)Oracle 9i 数据库设计指引全集(1)
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • .bat批处理(六):替换字符串中匹配的子串
  • .CSS-hover 的解释
  • .net 程序发生了一个不可捕获的异常
  • .NET 给NuGet包添加Readme
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .net6 webapi log4net完整配置使用流程
  • .Net7 环境安装配置
  • .NetCore部署微服务(二)
  • .net快速开发框架源码分享
  • .NET中的十进制浮点类型,徐汇区网站设计
  • /boot 内存空间不够