js获取当前月的开始日期和结束日期

2020-02-02  本文已影响0人  Hello杨先生
      //获取月份
      getAlldate(item){
        console.log(item)
        var now = new Date(); //当前日期
        var nowDayOfWeek = now.getDay(); //今天本周的第几天
        var nowDay = now.getDate(); //当前日
        // var nowMonth = now.getMonth(); //当前月
        var nowMonth = item-1; //当前月
        var nowYear = now.getYear(); //当前年
        nowYear += (nowYear < 2000) ? 1900 : 0; //
        var lastMonthDate = new Date(); //上月日期
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
        var lastYear = lastMonthDate.getYear();
        var lastMonth = lastMonthDate.getMonth();
        var monthStartDate = new Date(nowYear, nowMonth, 1);
        console.log('start_time',this.formatDate(monthStartDate))
        var days = new Date(nowDay, item, 0).getDate();
        console.log(days)
        var monthEndDate = new Date(nowYear, nowMonth, days);
        console.log('end_time',this.formatDate(monthEndDate))

        this.params.start_time = this.formatDate(monthStartDate)
        this.params.end_time = this.formatDate(monthEndDate)

        this.getTranRecord()  //调用获取数据方法

      },
      // 格式化日期
      formatDate(date) {
        var myyear = date.getFullYear();
        var mymonth = date.getMonth()+1;
        var myweekday = date.getDate();

        if(mymonth < 10){
          mymonth = "0" + mymonth;
        }
        if(myweekday < 10){
          myweekday = "0" + myweekday;
        }
        return (myyear+mymonth + myweekday);
      },

image.png
上一篇下一篇

猜你喜欢

热点阅读