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

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

TypeScript 复制代码
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: '上月'
      });
    }
  },

相差多少天:

TypeScript 复制代码
  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;
  },

格式化:

TypeScript 复制代码
 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}`;
  },
相关推荐
mCell4 小时前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip4 小时前
Node.js 子进程:child_process
前端·javascript
codingandsleeping10 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
白水清风11 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
用户221520442780011 小时前
new、原型和原型链浅析
前端·javascript
阿星做前端11 小时前
coze源码解读: space develop 页面
前端·javascript
叫我小窝吧11 小时前
Promise 的使用
前端·javascript
前端康师傅13 小时前
JavaScript 作用域
前端·javascript
云枫晖13 小时前
JS核心知识-事件循环
前端·javascript
eason_fan14 小时前
Git 大小写敏感性问题:一次组件重命名引发的CI构建失败
前端·javascript