js 获取本月第一天和最后一天,上月第一天和最后一天

  • 当前月第一天

 CurrentMonthFirst(){
       var date=new Date();
       date.setDate(1);
       return date;
  },
  • 当前月最后一天
CurrentMonthLast(){
      var date=new Date();
      var currentMonth=date.getMonth();
      var nextMonth=++currentMonth;
      var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
      var oneDay=1000*60*60*24;
      return new Date(nextMonthFirstDay-oneDay);
  },
  • 上月的第一天和最后一天
gettime2(){
     let nowdays = new Date();
     let year = nowdays.getFullYear();
     let month = nowdays.getMonth();
     if(month==0){
         month=12;
         year=year-1;
     }
     if (month < 10) {
          month = "0" + month;
     }
     let firstDayOfPreMonth = year + "-" + month + "-" + "01";
     let lastDay = new Date(year, month, 0);
     let lastDayOfPreMonth = year + "-" + month + "-" + lastDay.getDate();
     firstDayOfPreMonth = firstDayOfPreMonth.toString();
     lastDayOfPreMonth = lastDayOfPreMonth.toString();
     this.value4 = [];
     this.value4.push(firstDayOfPreMonth);
     this.value4.push(lastDayOfPreMonth);
  },

需要其他的时间格式,把获取的时间在自行转化一下就可以了。

你可能感兴趣的:(javaScript)