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

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

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}`;
  },
相关推荐
mg66819 分钟前
微信小程序入门实例_____打造你的专属单词速记小程序
微信小程序·小程序
【ql君】qlexcel20 分钟前
Notepad++ 复制宏、编辑宏的方法
开发语言·javascript·notepad++··宏编辑·宏复制
程序员陆通26 分钟前
Vibe Coding开发微信小程序实战案例
微信小程序·小程序·notepad++·ai编程
就改了1 小时前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_1 小时前
Ajax 入门
前端·javascript·ajax
奋飛1 小时前
TypeScript系列:第六篇 - 编写高质量的TS类型
javascript·typescript·ts·declare·.d.ts
sunbyte2 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟2 小时前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计
浏览器API调用工程师_Taylor2 小时前
AOP魔法:一招实现登录弹窗的全局拦截与动态处理
前端·javascript·vue.js
FogLetter2 小时前
初识图片懒加载:让网页像"懒人"一样聪明加载
前端·javascript