js获取N个月前的月份第一天和第二天

   //获取上N个月的时间
  , getDate(n) {
    let yearCount = Math.floor(n / 12);
    
    let modMonth = n % 12;
    let nowdays = new Date();
    let year = nowdays.getFullYear();
    year = year - yearCount
    let month = nowdays.getMonth();
    let _month = month - modMonth
    if (_month <= 0) {
      _month = 12 + _month
      year = year - 1
    }
    if (_month < 10) {
      _month = "0" + _month;
    }
    let firstDay = year + "-" + _month + "-" + "01";//上个月的第一天
    let myDate = new Date(year, _month, 0);
    let lastDay = year + "-" + _month + "-" + myDate.getDate();//上个月的最后一天
    console.log(firstDay)
    console.log(lastDay)
  }

你可能感兴趣的:(js获取N个月前的月份第一天和第二天)