获取本周第一天和最后一天&本月第一天和最后一天&本年第一天最后一天&昨天&前天日期

评论有小伙伴提出周日的时候获取的不对,我自己后来也发现了,周日的时候会获取到下周 周一-周日的日期,主要是date默认的一周是周日-周六,所以周日的时候date.getDay()获取到的是本周的第一天,而我的逻辑是按照周一--周日,所以本周日的时候会获取了下周一--下周日的日期

有需要的小伙伴可以按照下面调整一下逻辑为周日--周六为一周就没问题了

getCurrentDay(currentDate) {
      let thisweek = {}
      let thisMonth = {}
      let thisYear = {}
      let date = new Date();
      // 本周一的日期
      date.setDate(date.getDate() - date.getDay() + 1);
      thisweek.start_day = date.getFullYear() + "-" + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1)) + "-" + (date.getDate()<10?"0"+date.getDate():date.getDate()) ;
      // 本周日的日期
      date.setDate(date.getDate() - date.getDay() + 7);
      thisweek.end_day = date.getFullYear() + "-" + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1))  + "-" + (date.getDate()<10?"0"+date.getDate():date.getDate());

      // 本月第一天
      date.setDate(1);
      thisMonth.start_day = date.getFullYear() + "-" + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1))  + "-" + (date.getDate()<10?"0"+date.getDate():date.getDate())
      // 本月最后一天
      var lastDay = new Date(date.getFullYear(), date.getMonth() + 1 , 0)
      thisMonth.end_day = lastDay.getFullYear() + "-" + ((lastDay.getMonth() + 1)<10?"0"+(lastDay.getMonth()+1):(lastDay.getMonth() + 1))  + "-" + (lastDay.getDate()<10?"0"+lastDay.getDate():lastDay.getDate())

      // 本年第一天
      thisYear.start_day = date.getFullYear() + "-" + "01-01"
      // 本年最后一天
      thisYear.end_day = date.getFullYear() + "-" + "12-31"
      
      if(currentDate=="本周"){
        return thisweek
      }else if(currentDate=="本月"){
        return thisMonth
      }else if(currentDate=="本年"){}
        return thisYear
    }

百度了n个大神的方法终于整合了这一个方法

再补充一个获取昨天前天的方法

getDate(){
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
    let yesterday = year +"-"+ month +"-" + (date-1)
    let beforeYesterday = year +"-"+ month +"-" + (date-2)
    if(date=="01"){
      let day = new Date(nowDate.getFullYear(), nowDate.getMonth(), 0).getDate();
      yesterday = year +"-"+ ((month-1)<10?"0" + (month-1):(month-1)) +"-" + day
      beforeYesterday = year +"-"+ ((month-1)<10?"0" + (month-1):(month-1)) +"-" + (day-1)
    }else if(date=="02"){
      let day = new Date(nowDate.getFullYear(), nowDate.getMonth(), 0).getDate();
      beforeYesterday = year +"-"+ ((month-1)<10?"0" + (month-1):(month-1)) +"-" + day
    }
    this.setState({
      yesterday: yesterday,
      beforeYesterday: beforeYesterday
    })
  }

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值