获取昨日、近7天、近30天、本周、本月日期,回显到element的日期选择

在vue项目中,先在utils工具文件夹,新建date.js文件

function timeForMat(count) {
  // 拼接时间
  const time1 = new Date();
  const time2 = new Date();
  if (count === 1) {
    time1.setTime(time1.getTime() - 24 * 60 * 60 * 1000);
  } else {
    if (count >= 0) {
      time1.setTime(time1.getTime());
    } else {
      if (count === -2) {
        time1.setTime(time1.getTime() + 24 * 60 * 60 * 1000 * 2);
      } else {
        time1.setTime(time1.getTime() + 24 * 60 * 60 * 1000);
      }
    }
  }

  const Y1 = time1.getFullYear();
  const M1 = time1.getMonth() + 1 > 9 ? time1.getMonth() + 1 : '0' + (time1.getMonth() + 1);
  const D1 = time1.getDate() > 9 ? time1.getDate() : '0' + time1.getDate();
  //
  const timer1 = Y1 + '-' + M1 + '-' + D1 + ' ' + '23:59:59'; // 当前时间

  time2.setTime(time2.getTime() - 24 * 60 * 60 * 1000 * count);
  const Y2 = time2.getFullYear();
  const M2 = time2.getMonth() + 1 > 9 ? time2.getMonth() + 1 : '0' + (time2.getMonth() + 1);
  const D2 = time2.getDate() > 9 ? time2.getDate() : '0' + time2.getDate();
  //
  const timer2 = Y2 + '-' + M2 + '-' + D2 + ' ' + '00:00:00'; // 之前的7天或者30天
  return [timer2, timer1];
}
function add0(m) {
  return m < 10 ? '0' + m : m;
}

let dateModule = {
  days(num) {
    const timer = timeForMat(num);
    return timer;
  },
  thirtyDays() {
    // 获取最近30天
    const timer = timeForMat(30);
    return timer;
  },
  sevenDays() {
    // 获取最近7天
    const timer = timeForMat(7);
    return timer;
  },
  yesterday() {
    // 校验是不是选择的昨天
    const timer = timeForMat(1);
    return timer;
  },
  today() {
    const timer = timeForMat(0);
    return timer;
  },
  //本周
  getCurrentWeek: function() {
    let startStop = new Array();
    //获取当前时间
    let currentDate = new Date();
    //返回date是一周中的某一天
    let week = currentDate.getDay();
    //返回date是一个月中的某一天
    // let month = currentDate.getDate();
    //一天的毫秒数
    let millisecond = 1000 * 60 * 60 * 24;
    //减去的天数
    let minusDay = week != 0 ? week - 1 : 6;
    //alert(minusDay);
    //本周 周一
    let monday = new Date(currentDate.getTime() - minusDay * millisecond);
    //本周 周日
    let sunday = new Date(monday.getTime() + 6 * millisecond);
    let sy = monday.getFullYear();
    let sm = monday.getMonth() + 1;
    let sd = monday.getDate();
    let ey = sunday.getFullYear();
    let em = sunday.getMonth() + 1;
    let ed = sunday.getDate();
    // +' 00:00:00'
    //+' 23:59:59'
    let s = sy + '-' + add0(sm) + '-' + add0(sd); //开始
    let e = ey + '-' + add0(em) + '-' + add0(ed); //结束

    startStop.push(s);
    startStop.push(e);
    return startStop;
  },
  //本月
  getCurrentMonth: function() {
    //起止日期数组
    let startStop = new Array();
    //获取当前时间
    let currentDate = new Date();
    //获得当前月份0-11
    let currentMonth = currentDate.getMonth();
    //获得当前年份4位年
    let currentYear = currentDate.getFullYear();
    //求出本月第一天
    let firstDay = new Date(currentYear, currentMonth, 1);

    //当为12月的时候年份需要加1
    //月份需要更新为0 也就是下一年的第一个月
    if (currentMonth == 11) {
      currentYear++;
      currentMonth = 0; //就为
    } else {
      //否则只是月份增加,以便求的下一月的第一天
      currentMonth++;
    }

    //一天的毫秒数
    let millisecond = 1000 * 60 * 60 * 24;
    //下月的第一天
    let nextMonthDayOne = new Date(currentYear, currentMonth, 1);
    //求出上月的最后一天
    let lastDay = new Date(nextMonthDayOne.getTime() - millisecond);
    let sy = firstDay.getFullYear();
    let sm = firstDay.getMonth() + 1;
    let sd = firstDay.getDate();
    let ey = lastDay.getFullYear();
    let em = lastDay.getMonth() + 1;
    let ed = lastDay.getDate();
    // +' 00:00:00';
    // +' 23:59:59'/
    let s = sy + '-' + add0(sm) + '-' + add0(sd); //开始
    let e = ey + '-' + add0(em) + '-' + add0(ed); //结束

    startStop.push(s);
    startStop.push(e);

    return startStop;
  },

  //日期格式转化 yyyy-mm-dd
  dateTimeFormat(val) {
    let date = new Date(val);
    let datetime = undefined;
    let y = date.getFullYear();
    let mon = date.getMonth() + 1;
    let d = date.getDate();
    // let h = date.getHours();
    // let m = date.getMinutes();
    // let s = date.getSeconds();
    mon = mon < 10 ? '0' + mon : mon;
    d = d < 10 ? '0' + d : d;
    // h = h<10?'0'+h:h
    // m = m<10?'0'+m:m
    // s = s<10?'0'+s:s
    datetime = y + '-' + mon + '-' + d;

    return datetime;
  },
};

module.exports = dateModule;

二、在项目文件中应用 import dateModule from ‘@/utils/date’

案例: //默认日期昨天,
this.dateTime1 =[ new Date(dateModule.yesterday()[0]),new Date(dateModule.yesterday()[1]) ]

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玖逸少女梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值