获取本周日期,本月日期,上个月日期

this.date = '';
let date = new Date();
switch (val) {
  case '本周':
    let WeekFirstDay = new Date(date - (date.getDay() - 1) * 86400000);
    let WeekLastDay = new Date((WeekFirstDay / 1000 + 6 * 86400) * 1000);
    let M = Number(WeekLastDay.getMonth()) + 1;
    this.starTime = WeekFirstDay.getFullYear() + "-" + M + "-" + WeekFirstDay.getDate(); //本周第一天日期
    this.endTime = WeekLastDay.getFullYear() + "-" + M + "-" + WeekLastDay.getDate();//本周最后一天日期
    break;
  case '本月':
    let MonthFirstDay = new Date(date.getFullYear(), date.getMonth(), 1);
    let MonthNextFirstDay = new Date(date.getFullYear(), date.getMonth() + 1, 1);
    let MonthLastDay = new Date(MonthNextFirstDay - 86400000);
    let m = Number(MonthFirstDay.getMonth()) + 1;
    if(m<10) m='0'+m;
    this.starTime = MonthFirstDay.getFullYear() + "-" + m + '-01';  // 本月第一天
    this.endTime = MonthLastDay.getFullYear() + "-" + m + "-" + MonthLastDay.getDate(); //本月最后一天
    break;
  case '上个月':
    let year = date.getFullYear();
    let month = date.getMonth();
    if (month === 0) {
      month = 12;
      year = year - 1;
    }
    if (month < 10) {
      month = '0' + month;
    }
    let myDate = new Date(year, month, 0);
    this.starTime = year + '-' + month + '-01'; //上个月第一天
    this.endTime = year + '-' + month + '-' + myDate.getDate();//上个月最后一天
    break;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值