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

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

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}`;
  },
相关推荐
記億揺晃着的那天1 小时前
Vue + Element UI 表格自适应高度如何做?
javascript·vue.js·ui
GISer_Jing2 小时前
ByteDance——jy真题
前端·javascript·面试
真的想不出名儿2 小时前
Vue 中 props 传递数据的坑
前端·javascript·vue.js
阳光阴郁大boy2 小时前
星座运势网站技术解析:从零打造现代化Web应用
前端·javascript
sorryhc2 小时前
如何设计一个架构良好的前端请求库?
前端·javascript·架构
Queen_sy3 小时前
vue3 el-date-picker 日期选择器校验规则-选择日期范围不能超过七天
javascript·vue.js·elementui
lvchaoq3 小时前
react 修复403页面无法在首页跳转问题
前端·javascript·react.js
郝开3 小时前
6. React useState基础使用:useState修改状态的规则;useState修改对象状态的规则
前端·javascript·react.js
腾马科技4 小时前
新版saas餐饮外卖小程序源码/微信/支付宝/抖音/扫码点餐/DIY装修/美团代付/全开源
微信小程序·小程序·点餐小程序
技术钱5 小时前
react+andDesign+vite+ts从零搭建后台管理系统(三)-Layout布局
javascript·react.js·ecmascript