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

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

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}`;
  },
相关推荐
程序猿阿伟7 小时前
《首屏加载优化手册:Vue3+Element Plus项目提速的技术细节》
前端·javascript·vue.js
尘觉8 小时前
面试-浅复制和深复制?怎样实现深复制详细解答
javascript·面试·职场和发展
chxii10 小时前
ISO 8601日期时间标准及其在JavaScript、SQLite与MySQL中的应用解析
开发语言·javascript·数据库
没逛够10 小时前
Vue 自适应高度表格
javascript·vue.js·elementui
云起SAAS10 小时前
ai公司起名取名抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·ai公司起名取名
黑马源码库miui5208610 小时前
JAVA购物返利商品比价系统源码支持微信小程序
微信·微信小程序·小程序·1024程序员节
学会煎墙11 小时前
使用uniapp——实现微信小程序的拖拽排序(vue3+ts)
微信小程序·uni-app·vue·ts
咖啡の猫12 小时前
Vue初始化脚手架
前端·javascript·vue.js
拉不动的猪14 小时前
如何处理管理系统中(Vue PC + uni-app 移动端):业务逻辑复用基本方案
前端·javascript·架构
边洛洛14 小时前
next.js项目部署流程
开发语言·前端·javascript