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

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

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}`;
  },
相关推荐
2301_801074158 分钟前
TypeScript异常处理
前端·javascript·typescript
ᅠᅠᅠ@9 分钟前
异常枚举;
开发语言·javascript·ecmascript
caperxi11 分钟前
前端开发中的防抖与节流
前端·javascript·html
学习路上的小刘14 分钟前
vue h5 蓝牙连接 webBluetooth API
前端·javascript·vue.js
&白帝&14 分钟前
vue3常用的组件间通信
前端·javascript·vue.js
罗_三金35 分钟前
前端框架对比和选择?
javascript·前端框架·vue·react·angular
Fan_web1 小时前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html
叫我:松哥2 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
好名字08212 小时前
monorepo基础搭建教程(从0到1 pnpm+monorepo+vue)
前端·javascript
Flying_Fish_roe2 小时前
浏览器的内存回收机制&监控内存泄漏
java·前端·ecmascript·es6